PHP cURL鐨勮缁嗕娇鐢ㄦ墜鍐宊PHP鏁欑▼ | 甯涔嬪
浣跨敤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>
- 锕?nbsp;?php
- $phoneNumber = '13912345678';
- $message = 'This message was generated by curl and php';
- $curlPost = 'pNUMBER=' . urlencode($phoneNumber) .
- '&MESSAGE=' . urlencode($message) .
- '&SUBMIT=Send';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,
'http://www.example.com/sendSMS.php');- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS,
$curlPost);$data = curl_exec();- curl_close($ch);
- ?锕?nbsp;
浠庝笂闈㈢殑绋嬪簭鎴戜滑鍙互鐪嬪埌锛屼娇鐢–URLOPT_POST璁剧疆HTTP鍗忚鐨凱OST鏂规硶锛岃€屼笉鏄疓ET鏂规硶锛岀劧鍚庝互CURLOPT_POSTFIELDS璁剧疆POST鐨勬暟鎹€?/p>
鍏充簬浠g悊鏈嶅姟鍣?/p>
涓嬮潰鏄竴涓浣曚娇鐢ㄤ唬鐞嗘湇鍔″櫒鐨勭ず渚嬨€傝娉ㄦ剰鍏朵腑楂樹寒鐨勪唬鐮侊紝浠g爜寰堢畝鍗曪紝鎴戝氨涓嶇敤澶氳浜嗐€?/p>
- 锕?nbsp;?php $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,
'http://www.example.com');- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
- curl_setopt($ch, CURLOPT_PROXY,
'fakeproxy.com:1080');- curl_setopt($ch, CURLOPT_PROXYUSERPWD,
'user:password');- $data = curl_exec();
- curl_close($ch);
- ?锕?
鍏充簬鍏跺畠鏇村鐨勫唴瀹癸紝璇峰弬鐪嬬浉鍏崇殑PHP cURL鎵嬪唽銆?br />