国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php curl file_get_contents post方式獲取數(shù)據(jù)

2024-05-04 21:50:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

curl post,file_get_contents post,curl file_get_contents post請(qǐng)求數(shù)據(jù)

在PHP中cURL、file_get_contents函數(shù)均可以獲取遠(yuǎn)程鏈接的數(shù)據(jù),但是file_get_contents的可控制性不太好,對(duì)于各種復(fù)雜情況的數(shù)據(jù)采集情景,file_get_contents顯得有點(diǎn)無(wú)能為力,cURL在數(shù)據(jù)采集情景復(fù)雜的環(huán)境下略顯優(yōu)勢(shì)。cURL函數(shù)的curl_setopt里面還有很多參數(shù),讀者可以抽空整體看一遍,雖然平時(shí)未必用得上,但是至少做到心里有底,知道都有哪些參數(shù),必要時(shí)還能找出來(lái)使用。本文僅粗略介紹了file_get_contents函數(shù)和cURL函數(shù)的基本使用:

curl post方式獲取數(shù)據(jù),調(diào)用示例:

  1. $post_data = array ("category" => "9"); 
  2. echo postCurl('http://fity.cn/category.php',$post_data); 
  3.  
  4. //CURL函數(shù)--POST方式請(qǐng)求資源 
  5. function postCurl($api_url$post_data){ 
  6.     $ch = curl_init(); // 初始化CURL句柄 
  7.     curl_setopt($ch, CURLOPT_URL, $api_url); // 設(shè)置訪(fǎng)問(wèn)的url地址 
  8.     curl_setopt($ch, CURLOPT_TIMEOUT, 35); // 設(shè)置超時(shí) 
  9.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); // 等待的時(shí)間,如果設(shè)置為0,則不等待 
  10.     curl_setopt($ch, CURLOPT_HEADER, false); // 設(shè)定是否輸出頁(yè)面內(nèi)容 
  11.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 設(shè)定是否顯示頭信息 
  12.     curl_setopt($ch, CURLOPT_POST, true);  // post數(shù)據(jù) 
  13.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);// post的變量 
  14.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"); // 模擬瀏覽器頭信息 
  15.     curl_setopt($ch, CURLOPT_REFERER, "http://www.x.com"); // 偽造來(lái)源地址 
  16.     $data = curl_exec($ch); 
  17.     curl_close($ch); 
  18.   if ($data) { 
  19.     return $data
  20.   } else { 
  21.     return false; 
  22.   } 

file_get_contents post方式獲取數(shù)據(jù):

  1. $postdata = array ('category' => 9); 
  2. $postdata = http_build_query($postdata); 
  3. $opts = array ( 
  4.   'http' => array ( 
  5.   'method' => 'POST'
  6.   'content' => $postdata 
  7. //Vevb.com 
  8. ); 
  9. $context = stream_context_create($opts); 
  10. $html = file_get_contents('http://fity.cn/category.php', false, $context); 
  11. echo $html

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南昌市| 灵台县| 昌平区| 青龙| 金秀| 郸城县| 光泽县| 镇康县| 仪陇县| 兴隆县| 兴城市| 封丘县| 九龙城区| 宁阳县| 西宁市| 读书| 精河县| 沙洋县| 涿鹿县| 合江县| 南安市| 囊谦县| 太仓市| 伊吾县| 南阳市| 镇远县| 怀柔区| 青海省| 赫章县| 陇西县| 沙湾县| 富民县| 舞阳县| 诏安县| 辽源市| 轮台县| 龙岩市| 绍兴市| 茶陵县| 蕲春县| 淮安市|