Heim >Backend-Entwicklung >PHP-Tutorial >PHP cURL鐨勮缁嗕娇鐢ㄦ墜鍐宊PHP鏁欑▼ | 甯涔嬪

PHP cURL鐨勮缁嗕娇鐢ㄦ墜鍐宊PHP鏁欑▼ | 甯涔嬪

WBOY
WBOYOriginal
2016-07-15 13:32:031277Durchsuche

鍦?/li>

浣跨敤PHP cURL鍙互绠€鍗曞拰鏈夋晥鍦板幓鎶撶綉椤点€備綘鍙渶瑕佽繍琛屼竴涓剼鏈紝鐒跺悗鍒嗘瀽涓€涓嬩綘鎵€鎶撳彇鐨勭綉椤碉紝鐒跺悗灏卞彲浠ヤ互绋嬪簭鐨勬柟寮忓緱鍒颁綘鎯宠鐨勬暟鎹簡銆傛棤璁烘槸浣犳兂浠庝粠涓€涓摼鎺ヤ笂鍙栭儴鍒嗘暟鎹紝鎴栨槸鍙栦竴涓猉ML鏂囦欢骞舵妸鍏跺鍏ユ暟鎹簱锛岄偅鎬曞氨鏄畝鍗曠殑鑾峰彇缃戦〉鍐呭锛孭HP cURL鏄竴涓姛鑳藉己澶х殑PHP搴撱€傛湰鏂囦富瑕佽杩板鏋滀娇鐢ㄨ繖涓狿HP搴撱€?/p>

鍚敤PHP cURL璁剧疆

棣栧厛锛屾垜浠緱鍏堣纭畾鎴戜滑鐨凱HP鏄惁寮€鍚簡杩欎釜搴擄紝浣犲彲浠ラ€氳繃浣跨敤php_info()鍑芥暟鏉ュ緱鍒拌繖涓€淇℃伅銆?/p>

锕?phpphpinfo();?锕?br /> 

濡傛灉浣犲彲浠ュ湪缃戦〉涓婄湅鍒颁笅闈㈢殑杈撳嚭锛岄偅涔堣〃绀篜HP cURL搴撳凡琚紑鍚€?/p>

濡傛灉浣犵湅鍒扮殑璇濓紝閭d箞浣犻渶瑕佽缃綘鐨凱HP骞跺紑鍚繖涓簱銆傚鏋滀綘鏄湪Windows骞冲彴涓嬶紝閭d箞闈炲父绠€鍗曪紝浣犻渶瑕佹敼涓€鏀逛綘鐨刾hp.ini鏂囦欢鐨勮缃紝鎵惧埌php_curl.dll锛屽苟鍙栨秷鍓嶉潰鐨勫垎鍙锋敞閲婂氨琛屼簡銆傚涓嬫墍绀猴細

//鍙栨秷涓嬪湪鐨勬敞閲奺xtension=php_curl.dll
 

濡傛灉浣犳槸鍦↙inux涓嬮潰锛岄偅涔堬紝浣犻渶瑕侀噸鏂扮紪璇戜綘鐨凱HP浜嗭紝缂栬緫鏃讹紝浣犻渶瑕佹墦寮€缂栬瘧鍙傛暟——鍦╟onfigure鍛戒护涓婂姞涓?ldquo;–with-curl” 鍙傛暟銆?/p>

涓€涓皬绀轰緥

濡傛灉涓€鍒囧氨缁紝涓嬮潰鏄竴涓皬渚嬬▼锛?/p>

锕?php
// 鍒濆鍖栦竴涓狿HP cURL瀵硅薄
$curl = curl_init();

// 璁剧疆浣犻渶瑕佹姄鍙栫殑URL
curl_setopt($curl, CURLOPT_URL, 'http://cocre.com');

// 璁剧疆header
curl_setopt($curl, CURLOPT_HEADER, 1);

// 璁剧疆PHP cURL鍙傛暟锛岃姹傜粨鏋滀繚瀛樺埌瀛楃涓蹭腑杩樻槸杈撳嚭鍒板睆骞曚笂銆?br />curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// 杩愯cURL锛岃姹傜綉椤?br />$data = curl_exec($curl);

// 鍏抽棴URL璇锋眰
curl_close($curl);

// 鏄剧ず鑾峰緱鐨勬暟鎹?br />var_dump($data);
 

濡備綍POST鏁版嵁

涓婇潰鏄姄鍙栫綉椤电殑浠g爜锛屼笅闈㈠垯鏄悜鏌愪釜缃戦〉POST鏁版嵁銆傚亣璁炬垜浠湁涓€涓鐞嗚〃鍗曠殑缃戝潃http://www.example.com/sendSMS.php锛屽叾鍙互鎺ュ彈涓や釜琛ㄥ崟鍩燂紝涓€涓槸鐢佃瘽鍙风爜锛屼竴涓槸鐭俊鍐呭銆?/p>


 

<ol class="dp-xml">
<li class="alt"><span><span>锕?nbsp;?php  </span></span></li>
<li>
<span>$</span><span class="attribute">phoneNumber</span><span> = </span><span class="attribute-value">'13912345678'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">message</span><span> = </span><span class="attribute-value">'This message was generated by curl and php'</span><span>;</span>
</li>
<li class="alt">
<span>$</span><span class="attribute">curlPost</span><span> = </span><span class="attribute-value">'pNUMBER='</span><span> . urlencode($phoneNumber) . </span>
</li>
<li class="alt">
<span>'&</span><span class="attribute">MESSAGE</span><span>=</span><span class="attribute-value">' . urlencode($message) . </span>
</li>
<li class="alt">
<span class="attribute-value">'</span><span>&</span><span class="attribute">SUBMIT</span><span>=</span><span class="attribute-value">Send</span><span>';  </span>
</li>
<li>
<span>$</span><span class="attribute">ch</span><span> = </span><span class="attribute-value">curl_init</span><span>();  </span>
</li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_URL, <br>'http://www.example.com/sendSMS.php');  </span></li>
<li><span>curl_setopt($ch, CURLOPT_HEADER, 1);  </span></li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  </span></li>
<li><span>curl_setopt($ch, CURLOPT_POST, 1);  </span></li>
<li class="alt">
<span>curl_setopt($ch, CURLOPT_POSTFIELDS, <br>$curlPost);$</span><span class="attribute">data</span><span> = </span><span class="attribute-value">curl_exec</span><span>();  </span>
</li>
<li><span>curl_close($ch);  </span></li>
<li class="alt"><span>?锕?nbsp;</span></li>
</ol>

浠庝笂闈㈢殑绋嬪簭鎴戜滑鍙互鐪嬪埌锛屼娇鐢–URLOPT_POST璁剧疆HTTP鍗忚鐨凱OST鏂规硶锛岃€屼笉鏄疓ET鏂规硶锛岀劧鍚庝互CURLOPT_POSTFIELDS璁剧疆POST鐨勬暟鎹€?/p>

鍏充簬浠g悊鏈嶅姟鍣?/p>

涓嬮潰鏄竴涓浣曚娇鐢ㄤ唬鐞嗘湇鍔″櫒鐨勭ず渚嬨€傝娉ㄦ剰鍏朵腑楂樹寒鐨勪唬鐮侊紝浠g爜寰堢畝鍗曪紝鎴戝氨涓嶇敤澶氳浜嗐€?/p>

<ol class="dp-xml">
<li class="alt"><span><span>锕?nbsp;?php $</span><span class="attribute">ch</span><span> = </span><span class="attribute-value">curl_init</span><span>();  </span></span></li>
<li><span>curl_setopt($ch, CURLOPT_URL, <br>'http://www.example.com');  </span></li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_HEADER, 1);  </span></li>
<li><span>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  </span></li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);  </span></li>
<li><span>curl_setopt($ch, CURLOPT_PROXY, <br>'fakeproxy.com:1080');  </span></li>
<li class="alt"><span>curl_setopt($ch, CURLOPT_PROXYUSERPWD, <br>'user:password');  </span></li>
<li>
<span>$</span><span class="attribute">data</span><span> = </span><span class="attribute-value">curl_exec</span><span>();  </span>
</li>
<li class="alt"><span>curl_close($ch);  </span></li>
<li><span>?锕? </span></li>
</ol>

鍏充簬鍏跺畠鏇村鐨勫唴瀹癸紝璇峰弬鐪嬬浉鍏崇殑PHP cURL鎵嬪唽銆?br /> 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446168.htmlTechArticle鍦?浣跨敤PHP cURL鍙互绠€鍗曞拰鏈夋晥鍦板幓鎶撶綉椤点€備綘鍙渶瑕佽繍琛屼竴涓剼鏈紝鐒跺悗鍒嗘瀽涓€涓嬩綘鎵€鎶撳彇鐨勭綉椤碉紝鐒跺悗灏卞彲浠ヤ互绋嬪簭鐨勬柟寮忓緱鍒颁綘鎯宠鐨?..
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn