php json获取get请求,PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据...
PHP攜帶Cookie用Curl進(jìn)行Post或Get請(qǐng)求獲取數(shù)據(jù)
簡(jiǎn)單的curl請(qǐng)求(Get請(qǐng)求)
function hansCurl($url)
{
$url="https://www.yyob.com";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //設(shè)置傳輸?shù)?url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //發(fā)送 http 報(bào)頭
curl_setopt($ch, CURLOPT_COOKIE, $cookie); //設(shè)置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com"); //設(shè)置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"); //設(shè)置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解碼壓縮文件
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //調(diào)用cookie文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 對(duì)認(rèn)證證書(shū)來(lái)源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書(shū)中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 設(shè)置超時(shí)限制防止死循環(huán)
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
簡(jiǎn)單的curl請(qǐng)求(Post請(qǐng)求)
function hansCurl($url)
{
$url="https://www.yyob.com";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie="cookie";
$post_data = array(
"token" => "123456"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//設(shè)置傳輸?shù)?url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //發(fā)送 http 報(bào)頭
curl_setopt($ch, CURLOPT_COOKIE, $cookie);//設(shè)置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com");//設(shè)置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36");//設(shè)置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解碼壓縮文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// 對(duì)認(rèn)證證書(shū)來(lái)源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// 從證書(shū)中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 設(shè)置超時(shí)限制防止死循環(huán)
curl_setopt($ch, CURLOPT_POST, 1); //設(shè)置POST發(fā)送數(shù)據(jù)
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//發(fā)送POST數(shù)據(jù)內(nèi)容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //調(diào)用cookie文件
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
PHP的curl上傳文件(Post請(qǐng)求)
function hansCurl($url)
{
$url = "https://www.yyob.com";
$names="1.png";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$post_data = [
'name' => $names,
'attrFile' => new CURLFile(realpath('hanCURLFile/' . $names)),
];
$ch = curl_init(); // 啟動(dòng)一個(gè)CURL會(huì)話
curl_setopt($ch, CURLOPT_URL, $url); // 要訪問(wèn)的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 對(duì)認(rèn)證證書(shū)來(lái)源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書(shū)中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'); // 模擬用戶使用的瀏覽器
curl_setopt($ch, CURLOPT_POST, 1); // 發(fā)送一個(gè)常規(guī)的Post請(qǐng)求
curl_setopt($ch, CURLOPT_REFERER, 'https://www.vvhan.com'); // 自動(dòng)設(shè)置Referer
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // Post提交的數(shù)據(jù)包
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自動(dòng)跳轉(zhuǎn)
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 設(shè)置超時(shí)限制防止死循環(huán)
curl_setopt($ch, CURLOPT_HEADER, 0); // 顯示返回的Header區(qū)域內(nèi)容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //調(diào)用cookie文件
$output = curl_exec($ch); // 執(zhí)行操作
curl_close($ch); // 關(guān)閉CURL會(huì)話
return ($output);
}
?>
簡(jiǎn)單的file_get_contents請(qǐng)求(Get請(qǐng)求)
$url = 'https://www.yyob.com/';
$data = file_get_contents($url);
exit($data);
?>
總結(jié)
以上是生活随笔為你收集整理的php json获取get请求,PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: confluence启动不起来_汽车“一
- 下一篇: node js fork php,Nod