Returning Header as Array Using Curl

Returning header as array using Curl

Here, this should do it:

curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
curl_setopt($this->_ch, CURLOPT_HEADER, 1);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($this->_ch);
$info = curl_getinfo($this->_ch);

$headers = get_headers_from_curl_response($response);

function get_headers_from_curl_response($response)
{
$headers = array();

$header_text = substr($response, 0, strpos($response, "\r\n\r\n"));

foreach (explode("\r\n", $header_text) as $i => $line)
if ($i === 0)
$headers['http_code'] = $line;
else
{
list ($key, $value) = explode(': ', $line);

$headers[$key] = $value;
}

return $headers;
}

PHP curl() get all header at one time

You can get all headers from every request made until no Location header is sent using this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = curl_exec($ch);
curl_close($ch);

But then, you'll have to extract the information yourself because $headers is only a string, not an array.

If you only need the last location, simply do curl_getinfo($ch,CURLINFO_EFFECTIVE_URL).

How to return and format an array in php by using curl

That is JSON data...use json_decode() to convert to array

$arr = json_decode($content, TRUE);

$statuses = $arr['statuses'];

Get Header from PHP cURL response

It converts all headers into an array

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "example.com");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

$headers = [];
$output = rtrim($output);
$data = explode("\n",$output);
$headers['status'] = $data[0];
array_shift($data);

foreach($data as $part){

//some headers will contain ":" character (Location for example), and the part after ":" will be lost, Thanks to @Emanuele
$middle = explode(":",$part,2);

//Supress warning message if $middle[1] does not exist, Thanks to @crayons
if ( !isset($middle[1]) ) { $middle[1] = null; }

$headers[trim($middle[0])] = trim($middle[1]);
}

// Print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";

Can PHP cURL retrieve response headers AND body in a single request?

One solution to this was posted in the PHP documentation comments: http://www.php.net/manual/en/function.curl-exec.php#80442

Code example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ...

$response = curl_exec($ch);

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

Warning: As noted in the comments below, this may not be reliable when used with proxy servers or when handling certain types of redirects. @Geoffrey's answer may handle these more reliably.

Split cURL header info into array

Use this to split your header into an array

$myarray = array();
$data = explode("\n",$return);

$myarray['status'] = $data[0];

array_shift($data);

foreach($data as $part){
$middle = explode(":",$part);
$myarray[trim($middle[0])] = trim($middle[1]);
}

print_r($myarray);

As well as use curl_setopt($_h, CURLOPT_NOBODY, 1);
if you need to return only header.

More info can be found here

http://altafphp.blogspot.com/2012/04/get-http-headers-of-any-site-using-curl.html

Posting parameters and array in header in CURL using PHP

It is because you overwrite headers and because of that missing content type header. You must add that header to your headers array:

curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'Token: '.$token,
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($params)
));

edit: I've updated post to the working solution



Related Topics



Leave a reply



Submit