note 117138 added to book.curl

From: Date: Mon, 20 Apr 2015 22:30:25 +0000
Subject: note 117138 added to book.curl
Groups: php.notes 
Request: Send a blank email to php-notes+get-202307@lists.php.net to get a copy of this message
/* Example values url - 'http://example.com' fields - array('var' => 'value'), or can be empty auth - 'user:password', or can be empty by romet, 4.20.2015 */ function curl($url, $fields = array(), $auth = false){ $curl = curl_init($url); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_setopt($curl, CURLOPT_HEADER, 1); if($auth){ curl_setopt($curl, CURLOPT_USERPWD, "$auth"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if($fields){ $fields_string = http_build_query($fields); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string); } $response = curl_exec($curl); $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $header_string = substr($response, 0, $header_size); $body = substr($response, $header_size); $header_rows = explode(PHP_EOL, $header_string); $header_rows = array_filter($header_rows, trim); foreach((array)$header_rows as $hr){ $colonpos = strpos($hr, ':'); $key = $colonpos !== false ? substr($hr, 0, $colonpos) : (int)$i++; $headers[$key] = $colonpos !== false ? trim(substr($hr, $colonpos+1)) : $hr; } foreach((array)$headers as $key => $val){ $vals = explode(';', $val); if(count($vals) >= 2){ unset($headers[$key]); foreach($vals as $vk => $vv){ $equalpos = strpos($vv, '='); $vkey = $equalpos !== false ? trim(substr($vv, 0, $equalpos)) : (int)$j++; $headers[$key][$vkey] = $equalpos !== false ? trim(substr($vv, $equalpos+1)) : $vv; } } } //print_rr($headers); curl_close($curl); return array($body, $headers); } list($d['body'], $d['headers']) = curl2('http://google.com', array(q => '123')); //POST to google.com with POST var "q" as "123" echo '<pre>'; print_r($d); --------- OUTPUT: Array ( [headers] => Array ( [0] => HTTP/1.1 405 Method Not Allowed [Allow] => GET, HEAD [Date] => Mon, 20 Apr 2015 22:20:10 GMT [Server] => gws [Content-Length] => 1453 [X-Frame-Options] => SAMEORIGIN [Alternate-Protocol] => 80:quic,p=1 [Content-Type] => Array ( [0] => text/html [charset] => UTF-8 ) [X-XSS-Protection] => Array ( [1] => 1 [mode] => block ) ) [body] => <!DOCTYPE html>...etc ) ---- Server IP: 72.52.91.14 Probable Submitter: 217.71.46.18 ---- Manual Page -- http://php.net/manual/en/book.curl.php Edit -- https://master.php.net/note/edit/117138 Del: integrated -- https://master.php.net/note/delete/117138/integrated Del: useless -- https://master.php.net/note/delete/117138/useless Del: bad code -- https://master.php.net/note/delete/117138/bad+code Del: spam -- https://master.php.net/note/delete/117138/spam Del: non-english -- https://master.php.net/note/delete/117138/non-english Del: in docs -- https://master.php.net/note/delete/117138/in+docs Del: other reasons-- https://master.php.net/note/delete/117138 Reject -- https://master.php.net/note/reject/117138 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202307) next »