This article mainly introduced the PHP curl use example, this article directly gives an example, demonstrates the direct output to the browser and does not output directly to the browser different writing, needs the friend to be possible to refer to under
Overview
The previous two articles of this blog: Curl and Libcurl introduction as well as PHP using curl to use in PHP curl used to do a brief introduction, but the use of curl in PHP is not simple, especially the curl of various configuration items, this article will explain a few examples of PHP, So that we can better understand the curl.
Instances: Fetching pages
Using the Curl Crawl page is relatively straightforward, but one thing to note here is that curl will simply output the crawled page to the browser by default. However, we often encounter the situation is to get the content of the crawl, the content to do some processing before the operation. So, here are two different situations to write.
Direct output to Browser
The code is as follows:
$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 Baidu home page.
Do not output directly to the browser
If we do not want curl crawl content to be output directly to the browser, then we need to set curl "Curlopt_returntransfer" to be true so that the content Curl crawl will appear as the return value of the curl_exec () function.
The code is as follows:
$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 needs to be used in conjunction with Curlopt_binarytransfer.
* But in later versions of 5.1.3, this configuration item has been discarded.
*/
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 page source.