新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
phpcurl怎么只獲取數(shù)據(jù)
本文操作環(huán)境:windows7系統(tǒng)、php7.1版、DELL G3電腦

創(chuàng)新互聯(lián)專注于軹城企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,商城網(wǎng)站開發(fā)。軹城網(wǎng)站建設公司,為軹城等地區(qū)提供建站服務。全流程定制網(wǎng)站制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
php curl 怎么只獲取數(shù)據(jù)?
php 使用 CURL 獲取數(shù)據(jù)
第一種,POST 和 GET 合并
function http_curl($url, $type = 'get', $data = ''){
$cl = curl_init(); //初始化
curl_setopt($cl, CURLOPT_URL, $url); //設置 cURL 傳輸選項
curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1); // 將curl_exec()獲取的信息以字符串返回,而不是直接輸出。
curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, false);
if($type == 'post'){
curl_setopt($cl, CURLOPT_POST, 1); //發(fā)送 POST 請求,類型為:application/x-www-form-urlencoded
curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($cl); //執(zhí)行 cURL 會話
curl_close($cl);
return $output;
}
第二種 POST 和 GET分開
POST
$url = "http://localhost/web_services.php";
$post_data = array ("username" => "bob","key" => "12345");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post數(shù)據(jù)
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// post的變量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
//打印獲得的數(shù)據(jù)
print_r($output);
GET
//初始化
$ch = curl_init();
//設置選項,包括URL
curl_setopt($ch, CURLOPT_URL, "http://www.jb51.net");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//執(zhí)行并獲取HTML文檔內容
$output = curl_exec($ch);
//釋放curl句柄
curl_close($ch);
//打印獲得的數(shù)據(jù)
print_r($output);
以上方式獲取到的數(shù)據(jù)是json格式的
使用 json_decode($output,true)可解析為數(shù)組;使用 json_decode($output) 可解析為對象
參數(shù)說明:
網(wǎng)頁題目:phpcurl怎么只獲取數(shù)據(jù)
網(wǎng)站鏈接:http://www.5511xx.com/article/cdsdsjo.html


咨詢
建站咨詢
