首页 >后端开发 >php教程 >PHP语言中php curl的几种应用方式_PHP教程

PHP语言中php curl的几种应用方式_PHP教程

WBOY
WBOY原创
2016-07-15 13:28:29843浏览

我们通过对1. php curl的默认调用方法,get方式访问url

<ol class="dp-xml">
<li class="alt"><span><span>....     </span></span></li>
<li class="">
<span>    $</span><span class="attribute"><font color="#ff0000">ch</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">curl_init</font></span><span>();     </span>
</li>
<li class="alt"><span>    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置http头     </span></li>
<li class=""><span>    curl_setopt($ch, CURLOPT_ENCODING, "gzip" ); <br>//设置为客户端支持gzip压缩     </span></li>
<li class="alt"><span>    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30 ); <br>//设置连接等待时间     </span></li>
<li class=""><span>    curl_setopt($ch, CURLOPT_URL, $url );     </span></li>
<li class="alt"><span>    curl_exec( $ch );     </span></li>
<li class="">
<span>    if ($</span><span class="attribute"><font color="#ff0000">error</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">curl_error</font></span><span>($ch) ) {     </span>
</li>
<li class="alt"><span>        //出错处理     </span></li>
<li class=""><span>        return -1;     </span></li>
<li class="alt"><span>    }     </span></li>
<li class=""><span>    fclose($fp);       </span></li>
<li class="alt"><span>    </span></li>
<li class="">
<span>    $</span><span class="attribute"><font color="#ff0000">curl_code</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">curl_getinfo</font></span><span>($ch, CURLINFO_HTTP_CODE);<br>//获取http返回值     </span>
</li>
<li class="alt">
<span>    if( $</span><span class="attribute"><font color="#ff0000">curl_code</font></span><span> == 200 ) {     </span>
</li>
<li class=""><span>        //正常访问url     </span></li>
<li class="alt"><span>    }     </span></li>
<li class=""><span>    //异常     </span></li>
<li class="alt"><span>....    </span></li>
</ol>

2. 设置http header支持php curl访问lighttpd服务器

<ol class="dp-xml"><li class="alt"><span><span>$header[]= 'Expect:';  </span></span></li></ol>

3. 设置curl,只获取http header,不获取body:

<ol class="dp-xml">
<li class="alt"><span><span>curl_setopt($ch, CURLOPT_HEADER, 1);       </span></span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_NOBODY, 1);      </span></li>
</ol>

或者只获取body:

<ol class="dp-xml">
<li class="alt"><span><span>curl_setopt($ch, CURLOPT_HEADER, 0);   <br>// make sure we get the body     </span></span></li>
<li class=""><span>curl_setopt($ch, CURLOPT_NOBODY, 0);     </span></li>
</ol>

4. 访问虚拟主机,需设置Host

<ol class="dp-xml"><li class="alt"><span><span>$header[]= 'Host: '.$host;   </span></span></li></ol>

5. 使用post, put, delete等REStful方式访问url

<ol class="dp-xml">
<li class="alt"><span><span>post:   </span></span></li>
<li class=""><span> </span></li>
<li class="alt"><span>  curl_setopt($ch, CURLOPT_POST, 1 );   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>put, delete:   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");  <br>//或者PUT,需要服务器支持这些方法。  </span></li>
</ol>

6. php curl保存下载内容为文件

<ol class="dp-xml"><li class="alt"><span><span>curl_setopt($ch, CURLOPT_FILE, $fp);  </span></span></li></ol>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446438.htmlTechArticle我们通过对 1. php curl的默认调用方法,get方式访问url .... $ ch = curl_init (); curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//设置http头 curl_setopt($ch,CURLOPT_...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn