cURL ã©ã¤ãã©ãª
php ã«ã¯ cURL ライブラリ ããã£ã¦ããã®Cã©ã¤ãã©ãªã®ã©ããã¼ã使ããã¨ã§ã»ã¨ãã©ã®ãããã¯ã¼ã¯ãªã¯ã¨ã¹ããæ±ããã¨ãåºæ¥ãã
cURLã®æ¬å®¶ã¯ http://curl.haxx.se/ ã§ãããã§libcurl ãä¸å¼æºåãã¦Cè¨èªãããããã¯ã¼ã¯ãªã¯ã¨ã¹ããæ±ãããã ãã©ãè¯ãããããªãããCè¨èªèªä½ã®ä½¿ãæ¹ã«ãã©ãã«ãã¨ãèããããã
php ã®cURLã使ãã¡ãªãã
ããã±ã¼ã¸ã¨ãã¦æä¾ããã¦ãã¦ãã©ãã§ãç°¡åã«HTTPãªã¯ã¨ã¹ããæ±ããã¨ãåºæ¥ãã
php ã ãã§æ¸ãããHTTPãªã¯ã¨ã¹ãã©ã¤ãã©ãªã¯è¨è¨ãå¤èãã£ãããã¦ã¡ãã£ã¨ã»ã»ã»
file_get_contentsã§ãåºæ¥ããã ãã©ãæ¢ãã¦cURLã§ä½ãã®ããªããã¨ä¾¿å©ã ã£ãããããã
curl ã®åºæ¬çãªä½¿ãæ¹
php ã® libcurl é¢æ°ã¯Cè¨èªã®ããã¨åããããªæãã«ãªã£ã¦ãã¦ãåæåå¾ã«ãªãã·ã§ã³ã渡ãã
- curl ã åæå
- ãªãã·ã§ã³å¤ãè¨å®
- ãªãã·ã§ã³å¤ãè¨å®
- ãªãã·ã§ã³å¤ãè¨å®
- ãªãã·ã§ã³å¤ãè¨å®
- ãªãã·ã§ã³å¤ãè¨å®
- ãªã¯ã¨ã¹ãå®è¡
ãã®ãªãã·ã§ã³ããããã«ããã»ã»ã»â オプション
ãªãã·ã§ã³ããã£ã±ããã£ã¦ãé¢é£ãã人ãå¤ãããããªãã ãããã
ãªã®ã§ããã使ããããªHTTPãªã¯ã¨ã¹ãã®cURLãµã³ãã«ä½æãã¦ããã¾ããã
curl 㧠HTTP GET
æåã«ãç°¡åãªä¾ã§ããHTTP GET㧠åæåããªãã·ã§ã³å¤è¨å®ãå®è¡ããã®ã®æµããé ã«å ¥ãã¾ï½ã
<?php $url = "http://www.yahoo.co.jp"; $curl = curl_init($url); // åæåï¼ $options = array( // ãªãã·ã§ã³é å //HEADER CURLOPT_HTTPHEADER => array( ), //Method CURLOPT_HTTPGET => true,//GET ); //set options curl_setopt_array($curl, $options); /// ãªãã·ã§ã³å¤ãè¨å® // request $result = curl_exec($curl); // ãªã¯ã¨ã¹ãå®è¡ //print echo $result; ?>
ããã ããç°¡åã§ãããfile_get_contentsã§ï¼è¡ã§æ¸ãããã¨ãã¯ã¶ã¯ã¶ãããªãã¨ã·ãªããããããªãããã¤ã³ãã¯ã $optionså¤æ°ã«ãHTTPã®ãªã¯ã¨ã¹ãã«é¢ãããã¨ãã¬ã¹ãã³ã¹ã®åå¾ã«ã¤ãã¦ã®ãããããã¾ã¨ã¾ãã¨ãããã¨ã$optionå¤æ°ããä½ã£ã¦ãã¾ãã°ãã¨ã¯æ¥½ã§ãã
è¦ãããã¨ã¯åæåâãªãã·ã§ã³å¤âå®è¡ã§ãã
ãªãã·ã§ã³å¤ã¯åå©ç¨ãããããããé åå¤æ°ã«å ¥ãã¦ããã¾ãã
ä»»æã®HTTP ããããä»å ãããã¨ãåºæ¥ã
ãªãã·ã§ã³å¤ãå¤ããã¨ãããã追å ã§ãã¾ãããããã®ãªãã·ã§ã³ã§ä»»æã®ããããä»ãããã¾ãã
ãã¨ãã°ãCookieã Authorization ãªã©ãã§ãã¾ãã
<?php $options = array( //HEADER CURLOPT_HTTPHEADER => array( "Authorization: Bearer access_token", ), //Method CURLOPT_HTTPGET => true,//GET );
$optionããä¿åãã¦ããã°æ½è±¡åãå¯è½
ããã cURLã®ã¡ãªããã ã¨æããã ãcURLã«æ¸¡ããªãã·ã§ã³ãä½ãã ãã§ãHTTPãªã¯ã¨ã¹ãã®ä½¿ãåããåºæ¥ãã
<?php $options = my_something( $options ); if( $my_args ){ $options = next_must_fields( $options ); } if( $my_args2 ){ $options = some_fields( $options ); } curl_setopt_array($curl, $options); // request $result = curl_exec($curl);
ãã®ããã«ãoption ãã使ãã¾ããã¦ããã°ãæ¡ä»¶ã«å¿ãã¦ãHTTPãªã¯ã¨ã¹ããåå²ããã¦ã³ã¼ãã®å ±éåãåºæ¥ãã
cURL㧠HTTP POST
POSTããã¨ãã¯ã CURLOPT_POST ã®ãã©ã°ãã»ããããã ãã
CURLOPT_POST => true,//POST
以ä¸ãPOSTã§ãã¼ã¿ãéä¿¡ãããµã³ãã«ã
$optionså¤æ°ã®ä¸èº«ã ããå¤ãã£ã¦ãã¦ãã³ã¼ãã®æ®ãé¨åã¯GETã¨å ±éã§ãã
<?php $url = "http://www.yahoo.co.jp"; $curl = curl_init($url); $options = array( //HEADER CURLOPT_HTTPHEADER => array( 'Accept: application/json', ), //Method CURLOPT_POST => true,//POST //body CURLOPT_POSTFIELDS => http_build_query($post_args), ); //set options curl_setopt_array($curl, $options); // request $result = curl_exec($curl); echo $result; ?>
POSTã®æã¯ã CURLOPT_POST ãã»ãããã¦CURLOPT_POSTFIELDSã«ãã£ã¼ã«ãã»ããããã
注æç¹ã空ã®ããã£ãéä¿¡ããã¨ãï¼APIã®POSTã ããCallï¼ã®ãããªå ´åã§ããã£ã¼ã«ãã¯å¿ é ã空æåãã»ããããªãã¨Content-Length: -1 ãéä¿¡ãã¦ãã¾ãã
ãã¨ãã°ãJSONã®APIããªã¯ã¨ã¹ãã§æãã¤ããã¨ã
<?php $body = '{"sample":"Helloworld"}' $curl = curl_init($url); $options = [ // ãªãã·ã§ã³é å //HEADER CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', // âããæååã§HTTPãããå ¥ããã®ã§æ³¨æ ], CURLOPT_POST=>true, CURLOPT_POSTFIELDS=>$body, ]; //set options curl_setopt_array($curl, $options); // request $result = curl_exec($curl); echo $result;
cURL 㧠HTTP PUTãªã¯ã¨ã¹ã
http ã® put ãªã¯ã¨ã¹ãéãã¨ããã ãããåãã
次ã®ä¾ã¯ããµã¼ãã«PUTãªã¯ã¨ã¹ãã§JSONãéä¿¡ãã¦ããã
<?php $url = "http://www.yahoo.co.jp"; $curl = curl_init($url); $options = array( //HEADER CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', ), //Method CURLOPT_CUSTOMREQUEST => "PUT",//PUT //body CURLOPT_POSTFIELDS => json_encode($data), // æ¬æ ); //set options curl_setopt_array($curl, $options); // request $result = curl_exec($curl); echo $result;
cURL 㧠HTTP DELETE
HTTPã®DELETEã®å ´åã¯ãã¡ãã£ã¨ã ããªãã·ã§ã³ãéãã
HTTPã¡ã½ããåãç´æ¥æå®ããã
<?php CURLOPT_CUSTOMREQUEST => "DELETE",//DELETE
以ä¸ã®ä¾ã¯ãµã¼ãã¼ã«DELETEã¡ã½ãããéä¿¡ãã¦ããã
<?php $url = "http://www.yahoo.co.jp"; $curl = curl_init($url); /// $options = array( //HEADER CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', ), //Method CURLOPT_CUSTOMREQUEST => "DELETE",//DELETE //body ); //set options curl_setopt_array($curl, $options); // request $result = curl_exec($curl); echo $result;
HTTPã®ã¬ã¹ãã³ã¹ãè¦ãã
<?php // request $result = curl_exec($curl); //ãªã¯ã¨ã¹ãã«é¢ããæ å ± $info = curl_getinfo($curl);
curl_getinfo ã®çµæã¯é å
Array ( [url] => http://localhost/ [content_type] => text/html [http_code] => 200 [header_size] => 363 [request_size] => 48 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.001496 [namelookup_time] => 0.000795 [connect_time] => 0.000931 [pretransfer_time] => 0.000994 [size_upload] => 0 [size_download] => 44 [speed_download] => 29411 [speed_upload] => 0 [download_content_length] => 44 [upload_content_length] => 0 [starttransfer_time] => 0.001446 [redirect_time] => 0 [redirect_url] => [primary_ip] => ::1 [certinfo] => Array ( ) [primary_port] => 80 [local_ip] => ::1 [local_port] => 50279 )
ã¿ã¼ããã«ããå®è¡ããæã«curl ã³ãã³ãã®ãªãã·ã§ã³ã§ãåå¾ã§ããæ å ±ã¨åããã®ãåããã
転éé度ãæ¥ç¶æéã¨ã便å©ããã§ãã
ã¬ã¹ãã³ã¹ããããè¦ãã
ãªãã·ã§ã³ãã¤ãããã¨ã§ã HTTP ã®ã¬ã¹ãã³ã¹ããããä»å ããããããã®HTTPã¬ã¹ãã³ã¹ãè¦ããã¨ãåºæ¥ãã
<?php // $url = "http://localhost"; // $curl = curl_init($url); // $options = array(); $options[CURLOPT_HEADER] = true;//å®è¡çµæåå¾ // curl_setopt_array($curl, $options); // $result = curl_exec($curl);
å®è¡çµæ
takuya@rena:~/Desktop$ php test.php HTTP/1.1 200 OK Date: Sun, 27 Jul 2014 00:22:51 GMT Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y Content-Location: index.html.en Vary: negotiate TCN: choice Last-Modified: Fri, 17 Jan 2014 16:20:28 GMT ETag: "3f59f-2c-4f02ceb8dab00" Accept-Ranges: bytes Content-Length: 44 Content-Type: text/html Content-Language: en <html><body><h1>It works!</h1></body></html>
綺éºã«HTTPã®ã¬ã¹ãã³ã¹ããããå«ãããHTTPã®ã¬ã¹ãã³ã¹ãè¦ããããããã¯ä¾¿å©ã
ã¬ã¹ãã³ã¹ãããã¨ãã¬ã¹ãã³ã¹ããã£ãåå²ãããã å ¨é¨åããã¨ä¾¿å©ã ã£ããä¸ä¾¿ã ã£ããããã®ã§ãæ å ±ãåå²ãããã
<?php //ãªã¯ã¨ã¹ã $result = curl_exec($curl); //ãªã¯ã¨ã¹ãå®è¡å¾ $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $header = substr($result, 0, $header_size); $body = substr($result, $header_size);
curl æ±ããã¨å¤åå®å¿
cURLãæ±ããã¨ããããã¯ã¼ã¯ã«ã¢ã¯ã»ã¹ããããã°ã©ã ã¯æ®ã©cURLã§æ±ããã®ã§ãåµãè¾¼ããã¨ãå°ãªããããã ãã¦ã¼ã¶ã¼ãå°ãªãã®ã§ããã£ã¨æ å ±ãã»ãããªã¨æã£ãããã
sshãftp ã ftps ãªã©ãå種ãããã³ã«ãæ±ããã
ããã©ã¤ãã©ãªããããããfopen_wrapperã«é ¼ããªãã¦ãããã§ããã
åèè³æ
http://jp1.php.net/manual/ja/function.curl-setopt.php
2018-12-10
誤åä¿®æ£
2019-03-02
JSON ãBodyã«å ¥ãã¦ãªã¯ã¨ã¹ããæããã¨ãã®è¨è¿°ã追å