Home > php教程 > php手册 > PHP语言中php curl的几种应用方式

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:05:26
Original
1171 people have browsed it

我们通过对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>
Copy after login

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

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

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>
Copy after login

或者只获取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>
Copy after login

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

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

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>
Copy after login

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template