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

創(chuàng)新互聯(lián)專注于軹城企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站開(kāi)發(fā)。軹城網(wǎng)站建設(shè)公司,為軹城等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
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); //設(shè)置 cURL 傳輸選項(xiàng)
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 請(qǐng)求,類(lèi)型為:application/x-www-form-urlencoded
curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($cl); //執(zhí)行 cURL 會(huì)話
curl_close($cl);
return $output;
}
第二種 POST 和 GET分開(kāi)
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();
//設(shè)置選項(xiàng),包括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文檔內(nèi)容
$output = curl_exec($ch);
//釋放curl句柄
curl_close($ch);
//打印獲得的數(shù)據(jù)
print_r($output);
以上方式獲取到的數(shù)據(jù)是json格式的
使用 json_decode($output,true)可解析為數(shù)組;使用 json_decode($output) 可解析為對(duì)象
參數(shù)說(shuō)明:
標(biāo)題名稱:phpcurl怎么只獲取數(shù)據(jù)
轉(zhuǎn)載來(lái)于:http://fisionsoft.com.cn/article/cdsdsjo.html


咨詢
建站咨詢
