CURL返回的响应数组PHP格式不正确


CURL returned response array not well formatted PHP

我正在尝试从http头中获取每个元素。然而,curl返回的数组没有很好地生成

<?php
$url = "http://www.wdudes.com/";
$curl = curl_init();
curl_setopt_array( $curl, array(
    CURLOPT_HEADER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_NOBODY => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $url ) );
    $response = curl_exec($curl);
    curl_close($curl);
    $headers = explode( "'n", $response);
    print_r($headers);
?>

获得的输出为

Array ( 
[0] => HTTP/1.1 200 OK 
[1] => Date: Fri, 29 Apr 2016 10:35:20 GMT 
[2] => Content-Type: text/html; charset=UTF-8 
[3] => Connection: close 
)

我想将数组格式化为:

Array ( 
[0] => HTTP/1.1 200 OK 
[Date] => Fri, 29 Apr 2016 10:35:20 GMT 
[Content-Type] => text/html; charset=UTF-8 
[Connection] => close 
)

您实际上可以使用这种方式:

<?php
    $headers = array(
      "HTTP/1.1 200 OK ",
      "Date: Fri, 29 Apr 2016 10:35:20 GMT ",
      "Content-Type: text/html; charset=UTF-8 ",
      "Connection: close"
    );
    $better = array();
    foreach ($headers as $key => $value) {
      if ($key == 0)
        $better[] = $value;
      else {
        $current = explode(":", $value, 2);
        $better[trim($current[0])] = trim($current[1]);
      }
    }

输出:演示

Array
(
    [0] => HTTP/1.1 200 OK 
    [Date] => Fri, 29 Apr 2016 10:35:20 GMT
    [Content-Type] => text/html; charset=UTF-8
    [Connection] => close
)

这是基本逻辑。如果有更好、更具表演性的方式,欢迎他们。

此方法类似于Praveen的答案,但请确保将处理任何带有附加冒号的答案。

for($i = 0; $i < count($output); $i++){
    //If array item contains ": "
    if(strpos($output[$i], ': ') !== false){
        //New key is text before the colon
        $newKey = substr($output[$i], 0, strpos($output[$i], ': '));
        //New val is text after the colon
        $newVal = substr($output[$i], strpos($output[$i], ': ')+1, strlen($output[$i]));
        //Remove item from existing array
        $output[$i] = "";       
        //Write item to array using new key and value
        $output[$newKey] = $newVal;
    }
}

如果不需要,您可以选择删除"从现有阵列中删除项目"行。

然后,您可以使用print_r(array_filter($output))显示结果;

结果如下:

Original Array
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Fri, 29 Apr 2016 10:35:20 GMT
    [2] => Content-Type: text/html; charset=UTF-8
    [3] => Connection: close
)

New Array:
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => 
    [2] => 
    [3] => 
    [Date] =>  Fri, 29 Apr 2016 10:35:20 GMT
    [Content-Type] =>  text/html; charset=UTF-8
    [Connection] =>  close
)

Filtered Array:
Array
(
    [0] => HTTP/1.1 200 OK
    [Date] =>  Fri, 29 Apr 2016 10:35:20 GMT
    [Content-Type] =>  text/html; charset=UTF-8
    [Connection] =>  close
)

解决方案是:

  • 使用$flag变量跳过第一个元素,即HTTP/1.1 200 OK
  • 使用foreach循环遍历数组,在每次迭代中使用explode()函数获得新的(键,值)对,并使用新的键将新值推送到数组中的适当位置
  • 最后,使用数组的旧键取消设置旧元素

所以你的代码应该是这样的:

// Suppose $headers is your original array
$flag = true;
foreach($headers as $key => $value){
    if($flag){
        $flag = false;
        continue;
    }
    if(strlen(trim($value))){
        $component = explode(": ", $value);
        $headers[$component[0]] = $component[1];
        unset($headers[$key]);
    }else{
        unset($headers[$key]);
    }
}
// display $headers array
var_dump($headers);

试试这个代码快照:

$newArray = array();
foreach ($headers as $key => $value) {
if(preg_match('/[a-zA-Z]*[^:]*/',$value) and $key != 0){
 preg_match('/[a-zA-Z]*[^:]*/',$value, $newKey);
$newKey = $newKey[0];
$newArray[$newKey] = trim(str_replace($newKey.':','',$value));
 }else{
$newKey = 0;
    $newArray[0] = $value;
 }
}