Home > Backend Development > PHP Tutorial > Two basic examples of php CURL

Two basic examples of php CURL

WBOY
Release: 2016-08-08 09:24:49
Original
1014 people have browsed it

Start the preparation work. First, find extension=php_curl.dll in the php.ini file, remove the ";" in front of it, and enable curl support.

curl_init(); // Initialize curl
curl_close(); //Relationship curl
curl_exec(); //Perform curl operation
curl_setopt(int ch, string option, value) //Set curl options
option is the desired attribute, value is the corresponding value


option’s basic attributes are as follows:
CURLOPT_URL //Set the address to crawl the web page
CURLOPT_POST //Use php to perform httppost operation and set the option to a non-zero value
CURLOPT_POSTIELDS //All data of post operation of httppost operation
CURLOPT_PETURNTRANSFER //Whether to return the content obtained from the page, if the selected value is non-zero


One: Basic example

<code><span>$curl</span> = curl_init();                            <span>//</span>初始化curl对象
<span>$curl_setopt</span>(<span>$curl</span>,<span>CURLOPT_URL</span>,<span>'www.baidu.com'</span>);  <span>//</span>设置抓取页面的地址
<span>$curl_setopt</span>(<span>$curl</span>,<span>CURLOPT_HEADER</span>,<span>1</span>);   <span>//</span>把一个头部包含在其中输出,值要设为非零
<span>$response</span> = curl_exec(<span>$curl</span>);           <span>//</span>执行操作,运行curl
curl_close(<span>$curl</span>);                     <span>//</span>关闭操作 </code>
Copy after login

Two: post data

<code><span>$data</span> = <span>array</span>(<span>'name'</span>=><span>'trany'</span>,<span>'age'</span>=><span>'12'</span>);
<span>$curl</span> = curl_init();              <span>//初始化curl对象</span><span>$curl_setopt</span>(<span>$curl</span>,CURLOPT_URL,<span>'WWW.BAIDU.COM'</span>);   <span>//设置页面抓取地址</span><span>$curl_setopt</span>(<span>$curl</span>,CURLOPT_POST,<span>1</span>);                <span>//做httppost提交</span><span>$curl_setopt</span>(<span>$curl</span>,CURLOPT_POSTFIELDS,<span>$data</span>);      <span>//传递值</span><span>$response</span> = curl_exec(<span>$curl</span>);                     <span>//执行操作</span>
curl_close(<span>$curl</span>);                               <span>//关闭操作</span></code>
Copy after login

The above introduces two basic examples of php CURL, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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