2016-08-01 96 views
0
$ch = curl_init(); 
$url = "https://ttr.test.ru/"; 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$result = curl_exec($ch); 
curl_close($ch); 

curl返回下一個文本(頭):Curl返回標題 - 如何獲取值標題?

HTTP/1.1 202 Test 
Access-Control-Headers: X-PHP-TEST-1, X-PHP-TEST-2, X-PHP-TEST-3 
Content-Type: text/plain; charset=utf-8 
X-PHP-TEST-1: a43edefa03d32ee8a5183a0662bb82e6 
X-PHP-TEST-2: 85628 
X-PHP-TEST-3: 2434234234 
Date: Mon, 01 Aug 2016 01:03:25 GMT 

如何獲得價值HTTP/1.1(202),X-PHP-TEST-1X-PHP-TEST-2X-PHP-TEST-3

+2

http://stackoverflow.com/questions/10589889/returning-header-as -array-using-curl – Ghost

+0

@Ghost php對此沒有標準功能? –

+0

你需要捲起自己的 – Ghost

回答

0

您可以分割結果:

$ch = curl_init(); 
$url = "https://ttr.test.ru/"; 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 
curl_close($ch); 

$ret_headers = explode("\n", explode("\r\n\r\n", $result)[0]); 
$headers = []; 


array_shift($ret_headers); 

foreach ($ret_headers as $header_str) { 
    $h = explode(":", $header_str); 
    $headers[trim($h[0])] = trim($h[1]); 
} 

print_r($headers); 
0

在等待響應寫了他自己的版本:

$ch = curl_init(); 
$url = "https://auth.selcdn.ru/"; 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_NOBODY, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Auth-User:".UPLOAD_FILES__IMG2_MENU_CAFE__LOGIN, "X-Auth-Key:".UPLOAD_FILES__IMG2_MENU_CAFE__PASSWORD)); 

$result = curl_exec($ch); 
curl_close($ch); 
$arr1 = split("\r\n", $result); 
$arr2 = array(); 
foreach($arr1 as $key=>$val){ 
    if($key === 0){ //for get code HTTP 
     $el = explode(' ',$val); 
     $arr2[$el[0]] = $el[1]; 
    } 
    elseif($val != ''){ 
     $el = explode(': ',$val); 
     $arr2[$el[0]] = $el[1]; 
    } 
}