首页 >后端开发 >php教程 >curl的使用

curl的使用

WBOY
WBOY原创
2016-07-28 08:25:431375浏览

第一次使用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认证的,所以需加上以下代码

<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>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 curl的使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn