PHP Curl Usage Examples

Source: Internet
Author: User
Tags curl

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.