Home > Backend Development > PHP Tutorial > Several application methods of php curl in PHP language_PHP tutorial

Several application methods of php curl in PHP language_PHP tutorial

WBOY
Release: 2016-07-15 13:28:29
Original
863 people have browsed it

We access the url through the default calling method of 1. Get method

<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. Set http The header supports php curl to access the lighttpd server

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

3. Set curl to only get the http header, not the 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

Or just get the 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. To access the virtual host, you need to set Host

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

5. Use REStful methods such as post, put, delete, etc. to access the 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 saves the downloaded content as a file

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446438.htmlTechArticleWe access the url through the default calling method of 1. php curl, get method.... $ ch = curl_init (); curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//Set http header curl_setopt($ch,CURLOPT_...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template