PHP Curl Usage example, phpcurl instance
Overview
The first two articles of this blog: Curl and Libcurl Introduction and PHP using Curl in PHP use of curl to do a brief introduction, but the use of curl in PHP is not simple, especially the various configuration of curl, this article will explain a few examples of PHP, So that everyone can better understand curl.
Example: Crawling a page
Using Curl to crawl a page is relatively straightforward, but one thing to note here is that Curl defaults to outputting the crawled page directly to the browser. However, we often encounter the situation is to obtain the content of the crawl, the content to do some processing before the operation. So here are two different things to write about.
Direct output to Browser
Copy the Code code as follows:
<?php
$url = "www.baidu.com";
$ch =curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
Curl_exec ($ch);
Curl_close ($ch);
?>
Run the above code, we will directly see the homepage of Baidu.
Do not output directly to the browser
If we don't want the content of curl to be output directly to the browser, then we need to set Curl's "Curlopt_returntransfer" to true so that the contents of curl crawl will appear as the return value of the curl_exec () function.
Copy the Code code as follows:
<?php
$url = "www.baidu.com";
$content = ";
$ch =curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer,true);
/*
* According to the manual, it seems that the previous version of PHP5.1.3 also needs to be used in conjunction with Curlopt_binarytransfer,
* However, this configuration item has been deprecated in later versions of 5.1.3.
*/
curl_setopt ($ch, curlopt_binarytransfer,true);
$content =curl_exec ($ch);
Var_dump ($content);
Curl_close ($ch);
?>
Run the code, we can see the page output to get the Web page source.
http://www.bkjia.com/PHPjc/1026059.html www.bkjia.com true http://www.bkjia.com/PHPjc/1026059.html techarticle php Curl Usage examples, phpcurl instance overview of the first two articles of this blog: Curl and Libcurl Introduction and PHP using Curl in PHP use of curl to do a brief introduction, but the PHP curl ...