php json获取get请求,PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据...
PHP攜帶Cookie用Curl進行Post或Get請求獲取數據
簡單的curl請求(Get請求)
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); //設置傳輸的 url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //發送 http 報頭
curl_setopt($ch, CURLOPT_COOKIE, $cookie); //設置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com"); //設置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"); //設置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"); //調用cookie文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 對認證證書來源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 設置超時限制防止死循環
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
簡單的curl請求(Post請求)
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);//設置傳輸的 url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //發送 http 報頭
curl_setopt($ch, CURLOPT_COOKIE, $cookie);//設置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com");//設置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");//設置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解碼壓縮文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// 對認證證書來源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// 從證書中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 設置超時限制防止死循環
curl_setopt($ch, CURLOPT_POST, 1); //設置POST發送數據
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//發送POST數據內容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //調用cookie文件
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
PHP的curl上傳文件(Post請求)
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(); // 啟動一個CURL會話
curl_setopt($ch, CURLOPT_URL, $url); // 要訪問的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 對認證證書來源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查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); // 發送一個常規的Post請求
curl_setopt($ch, CURLOPT_REFERER, 'https://www.vvhan.com'); // 自動設置Referer
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // Post提交的數據包
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 設置超時限制防止死循環
curl_setopt($ch, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //調用cookie文件
$output = curl_exec($ch); // 執行操作
curl_close($ch); // 關閉CURL會話
return ($output);
}
?>
簡單的file_get_contents請求(Get請求)
$url = 'https://www.yyob.com/';
$data = file_get_contents($url);
exit($data);
?>
總結
以上是生活随笔為你收集整理的php json获取get请求,PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: confluence启动不起来_汽车“一
- 下一篇: freemarker java类_在fr