PHP's curl is not so complicated, Phpcurl is also complex _php tutorial

Source: Internet
Author: User

PHP's curl is not so complicated, phpcurl is complicated.


Many students feel the first two big (including me) when using curl, and look at this curl_setopt function completely puzzled, but after you spend 10 minutes to read my introduction I believe you can easily tease Php curl.

First, take a look at a Curl code (spend 10 seconds, glance over it, and then skip to the following article)

1 
 Php2 $data= "
 
  
   
 [...]

  
 "; 3 $tuCurl=curl_init ();4curl_setopt ($tuCurl, Curlopt_url, "https://example.com/path/for/soap/url/"); 5curl_setopt ($tuCurl, Curlopt_port, 443); 6curl_setopt ($tuCurl, Curlopt_verbose, 0); 7curl_setopt ($tuCurl, Curlopt_header, 0); 8curl_setopt ($tuCurl, Curlopt_sslversion, 3); 9curl_setopt ($tuCurl, Curlopt_sslcert,GETCWD() . "/client.pem"); Tencurl_setopt ($tuCurl, Curlopt_sslkey,GETCWD() . "/keyout.pem");  Onecurl_setopt ($tuCurl, Curlopt_cainfo,GETCWD() . "/ca.pem");  Acurl_setopt ($tuCurl, Curlopt_post, 1);  -curl_setopt ($tuCurl, Curlopt_ssl_verifypeer, 1);  -curl_setopt ($tuCurl, Curlopt_returntransfer, 1);  thecurl_setopt ($tuCurl, Curlopt_postfields,$data);  -curl_setopt ($tuCurl, Curlopt_httpheader,Array("Content-type:text/xml", "SOAPAction: \"/soap/action/query\ "", "Content-length:".)strlen($data)));  -  - $tuData= Curl_exec ($tuCurl);  + if(!curl_errno ($tuCurl)){  -   $info= Curl_getinfo ($tuCurl);  +   Echo' Took '.$info[' Total_time ']. ' Seconds to send a request to '.$info[' URL '];  A}Else {  at   Echo' Curl error: '. Curl_error ($tuCurl);  - }  -  -Curl_close ($tuCurl);  - Echo $tuData;  -?>

WTF, what the hell is this?

Want to learn this "high-end" usage?

First of all, I'm sure you know that most of the URLs start with HTTP, because they need to be transmitted via HTTP (Hypertext Transfer Protocol Http-hypertext Transfer Protocol), but the data transfer is not simply a "Hello" To the server on the matter, the sender in order to facilitate the recipient to understand the sender's actual intentions and know who the sender is also, the sender is often to send a lot of additional information to the recipient, just as the sender needs in the letter coat an envelope, the envelope is written with various sender information. All of these eventually merge into a thing called message, which forms the basis of the entire Internet.

Curl's job is to send these messages over the HTTP protocol (PHP Libcurl currently supports other protocols such as HTTPS, FTP, Telnet, etc.)

Now look at the code, actually the code did only five things

here's how to crawl and submit any page data using the Get and post methods
1 
 Php2     //Initialize3     $curl=curl_init ();4     //Set URL5curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com ');6     //setting returns the output obtained as a text stream7curl_setopt ($curl, Curlopt_returntransfer,true);8     //Execute Command9     $data= Curl_exec ($curl);Ten     //Close URL Request OneCurl_close ($curl); A     //show the data obtained -     Print_r($data); -?> the  - 
 Php -     //Initialize -     $curl=curl_init (); +     //Set URL -curl_setopt ($curl, Curlopt_url, ' http://www.baidu.com '); +     //setting returns the output obtained as a text stream Acurl_setopt ($curl, Curlopt_returntransfer,true); at     //Set Post submission -curl_setopt ($curl, Curlopt_post, 1); -     //setting up post data -curl_setopt ($curl, Curlopt_postfields,Array("Data" = "value"); -     //Execute Command -     $data= Curl_exec ($curl); in     //Close URL Request -Curl_close ($curl); to     //Print Data +     Print_r($data); -?>

Interested students can also refer to the official PHP document, learn more curl usage

http://www.bkjia.com/PHPjc/952776.html www.bkjia.com true http://www.bkjia.com/PHPjc/952776.html techarticle PHP's curl is not so complicated, Phpcurl is also complex many students in the first use of curl when feeling a first two big (including me), looking at the curl_setopt function of the whole ...

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