0

I am using Gcm for sending notifications and I have used curl for the same. Here is the script

$registrationId=  array('adkbvkasdjb');
$headers=array(
    'Authorization: key=' . GOOGLE_API_KEY,
    'Content-Type: application/json'
);
$sendData=array(
    'msg'=>'any message');
$url= GCM_URL;

$details=array( 
    'registration_ids'=>$registrationId,
    'time_to_live'=> 48*60*60,
    'data'=>$sendData,
);
$ch=  curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_HEADER, TRUE);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($details));

$result=  curl_exec($ch);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

$header = substr($result, 0, $header_size);

$body = substr($result, $header_size);

// closing the curl connection
curl_close($ch);

Now i am getting the header of the response as

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Thu, 16 Jan 2014 11:05:17 GMT
Expires: Thu, 16 Jan 2014 11:05:17 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked

Since I am not very good with regex and i searched a lot about a method that could parse the header of the response but all in vain.

Question:-
So i just wanna know is there anything or any method that could parse the header response as an array ?

Note:- I found a little about http_parse_header() but it need pcre to be installed so i am not sure whether it is installed on my REAL SERVER :)

2
  • Couldn't you just split by newline and : Commented Jan 16, 2014 at 11:47
  • Headers are separate by \r\n in the HTTP spec Commented Jan 16, 2014 at 11:52

1 Answer 1

1

You don't need any Regex to do it. If you read the first comment of parse_http_headers documentation on php.net, there is a little implementation if pecl_http lib is not found. Follow the link: http://www.php.net/manual/en/function.http-parse-headers.php#112986

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.