php中Curl函數常用的兩個例子_PHP教程

來源:互聯網
上載者:User
  例子, 類比登陸

代碼如下
/**
* 類比登陸
* $url 請求地址
* $post 需要POST的資料
* $cookie 登陸時取的的COOKIE
* $cookiejar cookie要存到的位置 例如/tmp/test.cookie
* $referer 上頁地址
*
**/
function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){
$tmpInfo = '';
//用來存放cookie的檔案
//初始化curl
$curl = curl_init();
//設定目標網址
curl_setopt($curl, CURLOPT_URL, $url);
//使用目前所用的瀏覽器代理
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
//如果有Ref參數,則設定Referer頭,否則自動化佈建Referer頭
if($referer) {
curl_setopt($curl, CURLOPT_REFERER, $referer);
} else {
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
}
//如果有post資料參數,則方法為POST,並且設定資料,否則為GET
if($post) {
//發送一個常規的POST請求,預設類型為:application/x-www-form-urlencoded,www.111cn.net表單提交
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
//如果有cookie參數,則設定
if($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
//如果有cookie檔案參數,則設定存取Cookie檔案名稱
if($cookiejar) {
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiejar);
}
//如果是302轉移,則返迴轉移後的網址及內容
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
//設定執行的最大秒數
curl_setopt($curl, CURLOPT_TIMEOUT, 100);
//返回內容中是否包含頭資訊
curl_setopt($curl, CURLOPT_HEADER, 0);
//把返回的結果存在檔案或者變數中,而不是直接顯示在瀏覽器
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//執行函數後的返回結果
$tmpInfo = curl_exec($curl);
//如果出錯,顯示錯誤資訊
if (curl_errno($curl)) {
$tmpInfo = '
錯誤:
'.curl_error($curl);
}
//關閉curl對象
curl_close($curl);
//返回結果
return $tmpInfo;
}

  例子、soap

代碼如下
function vcurlsoap($url, $SoapRequest, $SoapAction) {
$ch = curl_init (); //initiate the curl session
curl_setopt ( $ch, CURLOPT_URL, $url ); //set to url to post to
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); // return data in a variable
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $SoapRequest ); // post the xml
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 ); // set timeout in seconds
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
$header = array ("Content-Type: text/xml" );
$header [] = "Content-Length: ".strlen($SoapRequest);
if (! is_null ( $SoapAction ))
$header [] = 'SOAPAction: "' . $SoapAction . '"';
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
$xmlResponse = curl_exec ( $ch );
curl_close ( $ch );
return $xmlResponse;
}

http://www.bkjia.com/PHPjc/730240.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/730240.htmlTechArticle例子, 類比登陸 代碼如下 /** * 類比登陸 * $url 請求地址 * $post 需要POST的資料 * $cookie 登陸時取的的COOKIE * $cookiejar cookie要存到的位置 例如...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.