CURL返回的响应数组没有格式良好的PHP

时间:2016-04-29 13:04:07

标签: php arrays curl

我正在尝试从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 
)

4 个答案:

答案 0 :(得分:0)

你实际上可以这样使用:

<?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]);
      }
    }

输出: Demo

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
)

这是基本逻辑。如果有更好,更高效的方法,欢迎他们。

答案 1 :(得分:0)

这种方法类似于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;
    }
}

您可以选择从现有阵列中删除&#34;删除项目&#34;如果不需要就行。

然后,您可以使用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
)

答案 2 :(得分:0)

试试这段代码:

{$var|replace:'value="AA"':'value="'$string_b'"'}
{$var|replace:'value="AA"':'value="`$string_b`"'}
{$var|replace:'value="AA"':'value=""$string_b""'}

答案 3 :(得分:0)

解决方案是:

  • 使用$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);