当前位置:Gxlcms > PHP教程 > PHPcURL鐨勮缁嗕娇鐢ㄦ墜鍐宊PHP鏁欑▼|甯涔嬪

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

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


  1. 锕?nbsp;?php
  2. $phoneNumber = '13912345678';
  3. $message = 'This message was generated by curl and php';
  4. $curlPost = 'pNUMBER=' . urlencode($phoneNumber) .
  5. '&MESSAGE=' . urlencode($message) .
  6. '&SUBMIT=Send';
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL,
    'http://www.example.com/sendSMS.php');
  9. curl_setopt($ch, CURLOPT_HEADER, 1);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($ch, CURLOPT_POST, 1);
  12. curl_setopt($ch, CURLOPT_POSTFIELDS,
    $curlPost);$
    data = curl_exec();
  13. curl_close($ch);
  14. ?锕?nbsp;

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

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

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

  1. 锕?nbsp;?php $ch = curl_init();
  2. curl_setopt($ch, CURLOPT_URL,
    'http://www.example.com');
  3. curl_setopt($ch, CURLOPT_HEADER, 1);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  6. curl_setopt($ch, CURLOPT_PROXY,
    'fakeproxy.com:1080');
  7. curl_setopt($ch, CURLOPT_PROXYUSERPWD,
    'user:password');
  8. $data = curl_exec();
  9. curl_close($ch);
  10. ?锕?

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446168.htmlTechArticle鍦?浣跨敤PHP cURL鍙互绠€鍗曞拰鏈夋晥鍦板幓鎶撶綉椤点€備綘鍙渶瑕佽繍琛屼竴涓剼鏈紝鐒跺悗鍒嗘瀽涓€涓嬩綘鎵€鎶撳彇鐨勭綉椤碉紝鐒跺悗灏卞彲浠ヤ互绋嬪簭鐨勬柟寮忓緱鍒颁綘鎯宠鐨?..

人气教程排行