鍦?/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 />