PHP curl 常用操作

來源:互聯網
上載者:User

標籤:spn   peter   erp   city   session   type   替換   sap   test   

網頁內容替換

$curl = curl_init();curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com");//執行後不直接列印出來curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);$output = curl_exec($curl);curl_close($curl);echo str_replace("百度", "屌絲", $output);

通過webservice查詢北京當前的天氣

        $data = ‘theCityName=北京‘;$curlobj = curl_init();curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/webServices/WeatherWebService.asmx/getWeatherbyCityName");//不返回要求標頭資訊curl_setopt($curlobj, CURLOPT_HEADER, 0);//以檔案流形式輸出,不直接輸出curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);//post方式請求curl_setopt($curlobj, CURLOPT_POST, 1);//傳遞一個作為http ‘post’操作的所有資料的字串curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);//這句不寫會報錯(請求中包含user-agent頭的字串)--未將對象引用設定到對象的執行個體curl_setopt($curlobj,CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT‘]);curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencode;charset=utf-8","Content-length:".strlen($data)));$rtn = curl_exec($curlobj);if(!curl_errno($curlobj)){//$info = curl_getinfo($curlobj);//print_r($info);echo $rtn;}else{echo ‘Curl error‘.curl_error($curlobj);}curl_close($curlobj);    

把本地檔案上傳FTP伺服器

        $curlobj = curl_init();$localfile = ‘ftp01.php‘;$fp = fopen($localfile, ‘r‘);curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/ftp01_uploaded_01.php");curl_setopt($curlobj, CURLOPT_HEADER, 0);curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);//最大延遲多少秒curl_setopt($curlobj, CURLOPT_TIMEOUT, 300);curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//讓PHP為上傳做準備curl_setopt($curlobj, CURLOPT_UPLOAD, 1);//輸出檔案curl_setopt($curlobj, CURLOPT_INFILE, filesize($localfile));$rtn = curl_exec($curlobj);fclose($fp);if(!curl_errno($curlobj)){echo "uploaded successfully";}else{echo ‘curl  error:‘ . curl_error($curlobj);}

  從ftp伺服器下載一個檔案到本地

        $curlobj = curl_init();curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/downloaddemo.txt");curl_setopt($curlobj, CURLOPT_HEADER, 0);curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curlobj, CURLOPT_TIMEOUT, 300);curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//FTP使用者名稱:密碼$outfile = fopen(‘test.txt‘, ‘wb‘);//儲存到本地的檔案名稱//輸出檔案curl_setopt($curlobj, CURLOPT_FILE, $outfile);$rtn = curl_exec($curlobj);curl_close($curlobj);if(!curl_errno($curlobj)){//$info = curl_getinfo($curlobj);//print_r($info);echo $rtn;}else{echo ‘Curl error‘.curl_error($curlobj);}   

  下載網路上的HTTPS的資源

        $curlobj = curl_init();curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js");curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);//設定HTTPS支援date_default_timezone_set(‘PRC‘);curl_setopt($curlobj,CURLOPT_SSL_VERIFYPEER,0);$output = curl_exec($curlobj);curl_close($curlobj);echo $output;

  登入慕課網並下載個人空間頁面

        $data = ‘[email protected]&password=123&remember=1‘$curlobj = curl_init();curl_setopt($curlobj,CURLOPT_URL,"http://www.imooc.com/user/login");//設定訪問網頁的URLcurl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);//執行之後不直接列印出來//Cookie相關設定,這部分設定需要在所有會話之前設定date_default_timezone_set("PRC");curl_setopt($curlobj, CURLOPT_COOKIESESSION, true);curl_setopt($curlobj, CURLOPT_COOKIEFILE, "cookiefile");curl_setopt($curlobj, CURLOPT_COOKIEJAR,"cookiefile");curl_setopt($curlobj, CURLOPT_COOKIE, session_name() . ‘=‘ . session_id());curl_setopt($curlobj, CURLOPT_HEADER, 0);curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($curlobj, CURLOPT_POST, 1);curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencode;charset=utf-8","Content-length:".strlen($data)));curl_exec($curlobj);curl_setopt($curlobj, CURLOPT_POST, "http://www.imooc.com/sapce/index");curl_setopt($curlobj, CURLOPT_POST, 0);curl_setopt($curlobj, CURLOPT_HTTPHEADER, 0);curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type:text/xml"))curl_close($curlobj);echo $output;

  

PHP curl 常用操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.