PHP Curl Usage example, phpcurl instance _php tutorial

Source: Internet
Author: User

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 ...

  • Related Article

    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.