Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中转换数组中的响应头_Php_Regex_Parsing_Http Headers - Fatal编程技术网

如何在php中转换数组中的响应头

如何在php中转换数组中的响应头,php,regex,parsing,http-headers,Php,Regex,Parsing,Http Headers,我使用Gcm发送通知,我也使用curl发送通知。这是剧本 $registrationId= array('adkbvkasdjb'); $headers=array( 'Authorization: key=' . GOOGLE_API_KEY, 'Content-Type: application/json' ); $sendData=array( 'msg'=>'any message'); $url= GCM_URL; $details=array(

我使用Gcm发送通知,我也使用curl发送通知。这是剧本

$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);
现在,我得到响应的标题为

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
因为我对正则表达式不是很在行,我搜索了很多关于一个可以解析响应头的方法,但都是徒劳的

问题:-
我只想知道是否有任何东西或任何方法可以将报头响应解析为数组


注意:-我找到了一点关于
http\u parse\u header()
的信息,但是它需要安装pcre,所以我不确定它是否安装在我的真实服务器上:)

您不需要任何正则表达式来完成它。如果您阅读了php.net上parse_http_headers文档的第一条注释,那么如果找不到pecl_http lib,就会有一个小实现。 请点击链接:

您不能按换行符拆分吗?在HTTP规范中,标题由
\r\n
分隔