Various uses of PHP cURL

by Admin


Posted on August 24, 2014 at 9:00 PM

Tags : : Curl PHP


cURL function is effective when you want to have taken a variety of information and access to the outside of the site in php. 
To take the information in the HTTP request, there is also a function called file_get_contents. You can easily get the HTML by as follows, however, such as changing the request header, to customize the request method has limitations, cURL is useful if you want to such a customized is. However, since the hard time when you use less firm information, and fold in various ways.

More than 1 year has passed since last update.

cURL function is effective when you want to have taken a variety of information and access to the outside of the site in php.
To take the information in the HTTP request, there is also a function called file_get_contents. You can easily get the HTML by as follows, however, such as changing the request header, to customize the request method has limitations, cURL is useful if you want to such a customized is. However, since the hard time when you use less firm information, and fold in various ways.

file_get_contents how to use
<Php? 
$ Url  =  "Http://Www.Yahoo.Co.Jp/" ; 
$ Html  =  file_get_contents ( $ Url ); 
var_dump ( $ Html );

The following will summarize the use of the cURL from.

Now, HTML is output.

Basic Usage
<Php? 
$ Url  =  "Http://Www.Yahoo.Co.Jp/" ; 
$ Ch  =  curl_init ();  // Introduction

// Option 
curl_setopt ( $ Ch ,  CURLOPT_URL ,  $ Url );  
curl_setopt ( $ Ch ,  CURLOPT_RETURNTRANSFER ,  True ); 
$ Html  =   Curl_exec ( $ Ch ); 
var_dump ( $ Html ); 
curl_close ( $ Ch );  // End

To check the HTTP get information

HTTP information
<? Php

Url $  =  "Http://Www.Yahoo.Co.Jp/" ; 
$ Ch  =  curl_init ();  // Introduction

// Option 
curl_setopt ( $ Ch ,  CURLOPT_URL ,  $ Url );  
curl_setopt ( $ Ch ,  CURLOPT_RETURNTRANSFER ,  True ); 
$ Info  =  curl_getinfo ( $ Ch ); // get in this function 
curl_close ( $ Ch );  // End

var_dump ( $ Info ); 
>> 
Array ( 22 )  { 
  [ "Url" ] => 
  String ( 23 )  "Http://Www.Yahoo.Co.Jp/" 
  [ "content_type" ] => 
  NULL 
  [ "Http_code" ] => 
  int ( 0 ) 
  [ "header_size" ] => 
  int ( 0 ) 
  [ "Request_size" ] => 
  int ( 0 ) 
  [ "filetime" ] => 
  int ( 0 ) 
  [ "Ssl_verify_result" ] => 
  int ( 0 ) 
  [ "Redirect_count" ] => 
  int ( 0 ) 
  [ "Total_time" ] => 
  Float ( 0 ) 
  [ "Namelookup_time" ] => 
  Float ( 0 ) 
  [ "Connect_time" ] => 
  Float ( 0 ) 
  [ "Pretransfer_time" ] => 
  Float ( 0 ) 
  [ "Size_upload" ] => 
  Float ( 0 ) 
  [ "Size_download" ] => 
  Float ( 0 ) 
  [ "Speed_download" ] => 
  Float ( 0 ) 
  [ "Speed_upload" ] => 
  Float ( 0 ) 
  [ "Download_content_length" ] => 
  Float ( - 1 ) 
  [ "Upload_content_length" ] => 
  Float ( - 1 ) 
  [ "Starttransfer_time" ] => 
  Float ( 0 ) 
  [ "Redirect_time" ] => 
  Float ( 0 ) 
  [ "certinfo" ] => 
  Array ( 0 )  { 
  } 
  [ "Redirect_url" ] => 
  String ( 0 )  "" 
}

Frequently used options

Base option
curl_setopt ( $ Ch ,  CURLOPT_RETURNTRANSFER ,  True ); // be stored in the variable 
curl_setopt ( $ Ch ,  CURLOPT_HEADER ,  True );  // If the header also want to output

Perform a proxy connection

proxy connection
curl_setopt ( $ Ch ,  CURLOPT_PROXYPORT ,  3128 ); // access port 
curl_setopt ( $ Ch ,  CURLOPT_PROXY ,  "192.168.0.1" ); // IP address or URL

POST Submit

POST Submit
<Php?  
// array of data to Post 
$ Post_data  =  Array ( 'Id'  =>  "Id"  , 'Password' =>  "Pass"  , "Mode" => "Login" );

curl_setopt ( $ Ch , CURLOPT_POST ,  True );

// Set an array of data 
curl_setopt ( $ Ch ,  CURLOPT_POSTFIELDS ,  Http_build_query ( $ Post_data ));

Dealing with Cookie

Dealing with Cookie
<Php? 
// Create a file to a temporary file directory where you want to save the Cookie information 
$ tmp_path  =   tempnam ( sys_get_temp_dir (),  "CKI" );

// post a sequence of data 
$ Account_data  =  Array ( 'Id'  =>  "Id"  , 'Password' =>  "Pass"  , "Mode" => "Login" );

Url $  =  "Http://Www.Pixiv.Net/login.Php" ;

Ch $  =  curl_init ();  // Introduction

curl_setopt ( $ Ch ,  CURLOPT_URL ,  $ Url );  
curl_setopt ( $ Ch ,  CURLOPT_RETURNTRANSFER ,  True );

// POST send 
curl_setopt ( $ Ch , CURLOPT_POST ,  True ); 
curl_setopt ( $ Ch ,  CURLOPT_POSTFIELDS ,  Http_build_query ( $ Account_data ));


// Cookie reception 
// cookie options 
curl_setopt ( $ Ch , CURLOPT_COOKIEFILE , $ tmp_path ); 
curl_setopt ( $ Ch ,  CURLOPT_COOKIEJAR ,  $ tmp_path ); 
Curl_exec ( $ Ch ); // run 
curl_close ( $ Ch );  // End


Url $  =  "Http://Www.Pixiv.Net/mypage.Php" ; 
$ Ch  =  curl_init ();  // Introduction

curl_setopt ( $ Ch ,  CURLOPT_URL ,  $ Url );  
curl_setopt ( $ Ch ,  CURLOPT_RETURNTRANSFER ,  True );

// Cookie send 
// cookie options 
curl_setopt ( $ Ch , CURLOPT_COOKIEFILE , $ tmp_path ); 
curl_setopt ( $ Ch ,  CURLOPT_COOKIEJAR ,  $ tmp_path );

Html $  =  Curl_exec ( $ Ch ); // run 
curl_close ( $ Ch );

var_dump ( $ Html );

// Temporary file Delete 
Unlink ( $ tmp_path );

It adds a request header

It adds a request header
<Php? 
$ Headers  =  Array ( 
    "HTTP / 1.0" , 
    "Accept: Text / Html, Application / xhtml Tasu xml, Application / xml; Q = 0.9, * / *; Q = 0.8" , 
    "Accept-Encoding: gzip , Deflate " , 
    " Accept-Language: Ja, En-Us; Q = 0.7, En; Q = 0.3 " , 
    " Connection: Keep-Alive " , 
    " User-Agent: Mozilla / 5.0 (Macintosh; Intel Mac OS X 10.9 ; Rv: 26.0) Gecko / 20100101 Firefox / 26.0 " 
    );


Url $  =  "Http://Www.Ugtop.Com/spill.Shtml" ; 
$ Ch  =  curl_init ();  // start 
curl_setopt ( $ Ch ,  CURLOPT_URL ,  $ Url );  
curl_setopt ( $ Ch ,  CURLOPT_RETURNTRANSFER ,  True ); 
// header additional options 
curl_setopt ( $ Ch ,  CURLOPT_HTTPHEADER ,  $ headers );

Html $  =   Curl_exec ( $ Ch ); 
$ Html  =  Mb_convert_encoding ( $ Html , "UTF-8" , "EUC-JP" ); 
var_dump ( $ Html ); 
curl_close ( $ Ch );  // End

Anyway, the basic part is I think it was put together.

There are still a variety of options, but you leave pitched how likely become a reference URL because not be introduced.

Official manual
http://php.net/manual/ja/book.curl.php

List of options
http://jp2.php.net/curl_setopt

have us create a wrapper class of cURL
Http://Qiita.Com/mpyw/items/c65fb4ec4cef80909a47

How take over the CURL session of: PHP professional article
http://www.phppro.jp/phptips/archives/vol16/3



Comments


You must be loged to add a comment !