PHP與cURL

來源:互聯網
上載者:User

標籤:blog   http   ar   io   os   sp   on   資料   div   

CURL PHP API comes with a wide array of options and features. This allows users to fine tune the requests and the way that the responses are handled.

PHP CURL API帶來廣闊的選擇和特性,協助使用者處理請求和其他資訊。

步驟概覽

 

初始化CURL->設定選項->執行CURL->關閉CURL

Initialize

$ch = curl_init();

Set Options

curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_URL, true);

Execute

curl_exec($ch);

Close

curl_close($ch);

與file_get_contents

Earlier in this chapter it was shown how to access the Yahoo spelling service with
the file_get_contents function. The following code shows how to do the same with CURL. As you will notice, the code is a bit lengthier than the equivalent
file_get_contents version. Obviously, this is the cost you have to pay in exchange of the customizability of CURL. However, you will soon realize that the increased number of lines is negligible in comparison to what you can do with CURL.

與file_get_contents相比,在實現同樣功能的前提下,CURL的代碼量會更長。這是我們複雜化處理資料需要付出的代價。然而你講很快發現增加的代碼量相對於你的工作而言,根本無足輕重。

<?php/*//yahoo weather web service$url=‘http://api.wooyun.org/domain/cnpc.com‘;$xml = file_get_contents($url);$out=json_decode($xml);print_r($out);*/$url=‘http://api.wooyun.org/domain/cnpc.com‘;$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);//能夠接受傳回值curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);$res=json_decode(curl_exec($ch));curl_close($ch);print_r($res);?>

 

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.