php curl詳解

來源:互聯網
上載者:User
cURL介紹cURL 是一個利用URL文法規定來傳輸檔案和資料的工具,支援很多協議,如HTTP、FTP、TELNET等。最爽的是,PHP也支援 cURL 庫。本文將介紹 cURL 的一些進階特性,以及在PHP中如何運用它

基本結構(1)初始化

curl_init()

(2)設定變數

curl_setopt()

(3)執行並擷取結果

curl_exec()

(4)釋放cURL控制代碼

curl_close()

cURL實現Get和PostGet方式實現

//初始化$ch = curl_init();//設定選項,包括URLcurl_setopt($ch, CURLOPT_URL, "http://www.jb51.net");curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 0);//執行並擷取HTML文檔內容$output = curl_exec($ch);//釋放curl控制代碼curl_close($ch);//列印獲得的資料print_r($output);

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資料curl_setopt($ch, CURLOPT_POST, 1);// post的變數curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);$output = curl_exec($ch);curl_close($ch);//列印獲得的資料print_r($output);

以上方式擷取到的資料是json格式的,使用json_decode函數解釋成數組。

$output_array = json_decode($output,true);

如果使用json_decode($output)解析的話,將會得到object類型的資料。

ubuntu下錯誤的解決

PHP Fatal error: Call to undefined function curl_init() in /var/www/protected/config/functions.php on line 132

在終端命令視窗鍵入

sudo apt-get install php5-curlsudo service apache restart

  • 聯繫我們

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