第一次使用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:WINDOWSsystem32目錄下。上述文件若不存在,下載後拷貝至對應目錄下,重啟apache伺服器即可。 (PS:表示沒出過此類問題,該解決方案也是百度到的)
環境配置成功後,呼叫curl_exec方法可能會存在無回傳值的問題。首先確認你訪問的是否為https,curl訪問url時預設是進行ssl認證的,所以需加上以下程式碼
<code>curl_setopt(<span>$ch</span>, <span>CURLOPT_SSL_VERIFYPEER</span>, <span>false</span>); <span>//</span>不验证证书 curl_setopt(<span>$ch</span>, <span>CURLOPT_SSL_VERIFYHOST</span>, <span>false</span>); <span>//</span>不验证证书</code>
以下附上php利用curl模擬post請求的程式碼
<code><span><span>function</span><span>request_post</span><span>(<span>$url</span> = <span>''</span>, <span>$post_data</span> = array<span>()</span>)</span> {</span><span>if</span> (<span>empty</span>(<span>$url</span>) || <span>empty</span>(<span>$post_data</span>)) { <span>return</span><span>false</span>; } <span>$o</span> = <span>""</span>; <span>foreach</span> ( <span>$post_data</span><span>as</span><span>$k</span> => <span>$v</span> ) { <span>$o</span>.= <span>"$k="</span> . urlencode( <span>$v</span> ). <span>"&"</span> ; } <span>$post_data</span> = substr(<span>$o</span>,<span>0</span>,-<span>1</span>); <span>$postUrl</span> = <span>$url</span>; <span>$curlPost</span> = <span>$post_data</span>; <span>$ch</span> = curl_init();<span>//初始化curl</span> curl_setopt(<span>$ch</span>, CURLOPT_URL,<span>$postUrl</span>);<span>//抓取指定网页</span> curl_setopt(<span>$ch</span>, CURLOPT_POST, <span>1</span>);<span>//post提交方式</span> curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$curlPost</span>); curl_setopt(<span>$ch</span>, CURLOPT_SSL_VERIFYPEER, <span>false</span>); <span>//不验证证书</span> curl_setopt(<span>$ch</span>, CURLOPT_SSL_VERIFYHOST, <span>false</span>); <span>//不验证证书</span><span>$data</span>=curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>return</span><span>$data</span>; }</code>
以上就介紹了 curl的使用,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。