curl的使用

來源:互聯網
上載者:User
第一次使用php的curl開發時可能出現“Call to undefined function curl_init()”的問題,表示php未配置curl的執行環境。
1、確認php.ini中extension=php_curl.dll前的分號是否已經去掉。去掉後重啟apache伺服器。
2、如果還存在相同問題,則需將php目錄下的libeay32.dll和ssleay32.dll檔案已經ext目錄下的php_curl.dll,拷貝至C:\WINDOWS\system32目錄下。上述檔案若不存在,下載後拷貝至相應目錄下,重啟apache伺服器即可。(PS:表示沒出過此類問題,該解決方案也是百度到的)

環境配置成功後,調用curl_exec方法可能會存在無傳回值的問題。首先確認你訪問的是否為https,curl訪問url時預設是進行ssl認證的,所以需加上以下代碼

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不驗證認證curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不驗證認證

以下附上php利用curl類比post請求的代碼

functionrequest_post($url = '', $post_data = array()) {if (empty($url) || empty($post_data)) {            returnfalse;        }        $o = "";        foreach ( $post_dataas$k => $v )         {             $o.= "$k=" . urlencode( $v ). "&" ;        }        $post_data = substr($o,0,-1);        $postUrl = $url;        $curlPost = $post_data;        $ch = curl_init();//初始化curl        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不驗證認證        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不驗證認證$data=curl_exec($ch);        curl_close($ch);        return$data;    }

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介紹了 curl的使用,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.