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

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

Jun 13, 2016 am 11:05 AM
curlgetphpseveral kindsrightapplicationusWaymethodoftransferpassdefault

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor