一波PHP中cURL庫(kù)的常見(jiàn)用法代碼示例
來(lái)源:易賢網(wǎng) 閱讀:784 次 日期:2016-08-24 14:58:29
溫馨提示:易賢網(wǎng)小編為您整理了“一波PHP中cURL庫(kù)的常見(jiàn)用法代碼示例”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了一波PHP中cURL庫(kù)的常見(jiàn)用法代碼示例,類Unix世界的cURL內(nèi)置于PHP中,使Linux和Mac OS用戶倍感親切,需要的朋友可以參考下

php 的CURL是不錯(cuò)的功能,下面收藏幾段不錯(cuò)的片段

0、基本例子

一般流程:

$to_url=$_GET['url'];

print_r($_GET);

if(substr($to_url,0,1)=='/'){

 $to_url="http://www.amazon.com".$to_url;

}

echo $to_url;

//初始化

$ch = curl_init();

//設(shè)置選項(xiàng),包括URL

curl_setopt($ch, CURLOPT_URL, $to_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

//執(zhí)行并獲取HTML文檔內(nèi)容

$output = curl_exec($ch);

$output=preg_replace("#href=\"#","href=\"http://in2.qq-ex.com/amazon.php?url=",$output);

// 釋放curl句柄

curl_close($ch);

echo $output;

// 指定代理地址

curl_setopt($ch, CURLOPT_PROXY, '11.11.11.11:8080');

// 如果需要的話,提供用戶名和密碼

curl_setopt($ch, CURLOPT_PROXYUSERPWD,'user:pass');

1、測(cè)試網(wǎng)站是否運(yùn)行正常

if (isDomainAvailible('http://gz.itownet.cn')) 

 { 

   echo "Up and running!"; 

 } 

 else

 { 

   echo "Woops, nothing found there."; 

 } 

 //returns true, if domain is availible, false if not 

 function isDomainAvailible($domain) 

 { 

   //check, if a valid url is provided 

   if(!filter_var($domain, FILTER_VALIDATE_URL)) 

   { 

     return false; 

   } 

   //initialize curl 

   $curlInit = curl_init($domain); 

   curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10); 

   curl_setopt($curlInit,CURLOPT_HEADER,true); 

   curl_setopt($curlInit,CURLOPT_NOBODY,true); 

   curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); 

   //get answer 

   $response = curl_exec($curlInit); 

   curl_close($curlInit); 

   if ($response) return true; 

   return false; 

 } 

2、可以代替file_gecontents的操作

function file_get_contents_curl($url) { 

 $ch = curl_init(); 

 curl_setopt($ch, CURLOPT_HEADER, 0); 

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 

 curl_setopt($ch, CURLOPT_URL, $url); 

 $data = curl_exec($ch); 

 curl_close($ch); 

 return $data; 

3、保存某個(gè)網(wǎng)站下的所有圖片

function getImages($html) { 

 $matches = array(); 

 $regex = '~http://somedomain.com/images/(.*?)\.jpg~i'; 

 preg_match_all($regex, $html, $matches); 

 foreach ($matches[1] as $img) { 

  saveImg($img); 

 } 

function saveImg($name) { 

 $url = 'http://somedomain.com/images/'.$name.'.jpg'; 

 $data = get_data($url); 

 file_put_contents('photos/'.$name.'.jpg', $data); 

$i = 1; 

$l = 101; 

while ($i < $l) { 

 $html = get_data('http://somedomain.com/id/'.$i.'/'); 

 getImages($html); 

 $i += 1; 

4、FTP應(yīng)用

// open a file pointer 

$file = fopen("/path/to/file", "r"); 

// the url contains most of the info needed 

$url = "ftp://username:password@mydomain.com:21/path/to/new/file"; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// upload related options 

curl_setopt($ch, CURLOPT_UPLOAD, 1); 

curl_setopt($ch, CURLOPT_INFILE, $fp); 

curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/path/to/file")); 

// set for ASCII mode (e.g. text files) 

curl_setopt($ch, CURLOPT_FTPASCII, 1); 

$output = curl_exec($ch); 

curl_close($ch); 

5、使用curl發(fā)送JSON數(shù)據(jù)

$data = array("name" => "Hagrid", "age" => "36");

$data_string = json_encode($data);

$ch = curl_init('http://api.local/rest/users');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

  'Content-Type: application/json',

  'Content-Length: ' . strlen($data_string))

);

$result = curl_exec($ch);

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:一波PHP中cURL庫(kù)的常見(jiàn)用法代碼示例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)