>백엔드 개발 >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으로 문의하세요.