[php]代码库
$html = file_get_contents('http://www.baidu.com');
print_r($http_response_header);
$fp = fopen('http://www.baidu.com', 'r');
print_r(stream_get_meta_data($fp));
fclose($fp);
$url = 'http://192.168.1.1/test.php';
$data = array(
'keyword' => 'test data',
);
$content = http_build_query($data);
$content_length = strlen($content);
$options = array(
'http' => array(
'method' => 'post',
'header' =>
"content-type: application/x-www-form-urlencoded\r\n" .
"content-length: $content_length\r\n",
'content' => $content
)
);
echo file_get_contents($url, false, stream_context_create($options));
<?php
function geturl($url){
$headerarray =array("content-type:application/json;","accept:application/json");
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch,curlopt_httpheader,$headerarray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}
function posturl($url,$data){
$data = json_encode($data);
$headerarray =array("content-type:application/json;charset='utf-8'","accept:application/json");
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_ssl_verifypeer, false);
curl_setopt($curl, curlopt_ssl_verifyhost,false);
curl_setopt($curl, curlopt_post, 1);
curl_setopt($curl, curlopt_postfields, $data);
curl_setopt($curl,curlopt_httpheader,$headerarray);
curl_setopt($curl, curlopt_returntransfer, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output,true);
}
function puturl($url,$data){
$data = json_encode($data);
$ch = curl_init(); //初始化curl句柄
curl_setopt($ch, curlopt_url, $url); //设置请求的url
curl_setopt ($ch, curlopt_httpheader, array('content-type:application/json'));
curl_setopt($ch, curlopt_returntransfer,1); //设为true把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, curlopt_customrequest,"put"); //设置请求方式
curl_setopt($ch, curlopt_postfields, $data);//设置提交的字符串
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
}
function delurl($url,$data){
$data = json_encode($data);
$ch = curl_init();
curl_setopt ($ch,curlopt_url,$put_url);
curl_setopt ($ch, curlopt_httpheader, array('content-type:application/json'));
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_customrequest, "delete");
curl_setopt($ch, curlopt_postfields,$data);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
}
function patchurl($url,$data){
$data = json_encode($data);
$ch = curl_init();
curl_setopt ($ch,curlopt_url,$url);
curl_setopt ($ch, curlopt_httpheader, array('content-type:application/json'));
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_customrequest, "patch");
curl_setopt($ch, curlopt_postfields,$data); //20170611修改接口,用/id的方式传递,直接写在url中了
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
return $output;
}
?>
function sendcurl($url, $data = null,$method='post')
{
$method=strtoupper($method);
$start_wdmcurl_time = microtime(true);
$header = array(' application/x-www-form-urlencoded');
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_failonerror, false);
// https 请求
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https")
{
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
}
if($method=='get'){
if($data && is_array($data) && count($data)>0 ){
$url.="?".http_build_query($data);
}
curl_setopt($ch, curlopt_url, $url);
}elseif($method=='post'){
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $data);
if (is_array($data) && count($data)>0)
{
curl_setopt($ch, curlopt_post, true);
$ispostmultipart = false;
foreach ($data as $k => $v)
{
if ('@' == substr($v, 0, 1))
{
$ispostmultipart = true;
break;
}
}
unset($k, $v);
if ($ispostmultipart) {
curl_setopt($ch, curlopt_postfields, $data);
} else {
curl_setopt($ch, curlopt_postfields, http_build_query($data));
}
}
}elseif(in_array($method,['put','delete','patch'])){
curl_setopt($ch, curlopt_customrequest,$method);
curl_setopt($ch, curlopt_postfields, $data);
}
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch,curlopt_httpheader,$header);
$reponse = curl_exec($ch);
curl_close($ch);
return $reponse;
}
composer require guzzlehttp/guzzle
$client = new \guzzlehttp\client();
$response = $client->request('get', 'https://api.github.com/repos/guzzle/guzzle');
echo $response->getstatuscode(); // 200
echo $response->getheaderline('content-type'); // 'application/json; charset=utf8'
echo $response->getbody(); // '{"id": 1420053, "name": "guzzle", ...}'
// send an asynchronous request.
$request = new \guzzlehttp\psr7\request('get', 'http://httpbin.org');
$promise = $client->sendasync($request)->then(function ($response) {
echo 'i completed! ' . $response->getbody();
});
$promise->wait();