PHP curl Use

Example: fetch page.

Using curl to fetch page is relatively simple, but there is one thing to note is that, by default curl will crawl the page output directly to the browser. However, we often encounter is get content to crawl, and then to operate for the contents do some processing. So, here to write two different situations.

Direct output to the browser

<?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 Baidu home page.

Not output directly to the browser

If we do not want to crawl content curl output directly to the browser, then you need to set the curl of "CURLOPT_RETURNTRANSFER" true, so curl crawl contents as curl_exec () return value of the function appears.

? < PHP 

 $ URL = "www.baidu.com" ; 

 $ Content = '' ; 

 $ CH = curl_init (); 

 curl_setopt ( $ CH , CURLOPT_URL to, $ URL ); 

 curl_setopt ( $ CH , CURLOPT_RETURNTRANSFER, TRUE ); 

 / * 

  * according to the manual of view, it seems PHP5.1.3 using previous versions also need to meet CURLOPT_BINARYTRANSFER together, 

  * but future versions 5.1.3, the configuration item has been abandoned. 

  * / 

 // curl_setopt ($ CH, CURLOPT_BINARYTRANSFER, TRUE); 

 $ Content = curl_exec ( $ CH ); 

 var_dump ( $ Content ); 

 curl_close ( $ CH );

 ?>

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11208028.html