2012-07-25 101 views
12

我一直在搜索小時,並且無法在此找到任何內容。我正在做一個php curl post請求到sugarsync API,並且它返回了我需要的頭文件中的一個位置。我不知道如何獲得這些信息。我必須保留它作爲一個職位,因爲我發佈一個XML文件到他們的API,他們所做的就是返回頭信息。我不知道如何訪問標題中的位置。 according to them我需要把它放入另一個xml文件併發布。任何幫助表示讚賞。從php curl post請求中獲取標題信息

回答

2

獲取響應中的標題信息。

curlsetopt($ch,CURLOPT_HEADER,true); 
+1

給我的標題信息。但如何解析它具體獲取位置? – selanac82 2012-07-25 22:16:50

15

如果您設置捲曲選項CURLOPT_FOLLOWLOCATION,cURL將爲您遵循位置重定向。

如果您想獲取標題,請將選項CURLOPT_HEADER設置爲1,並且您從curl_exec()返回的HTTP響應將包含標題。你可以解析他們的位置。

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, 1); // return HTTP headers with response 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the response rather than output it 

$resp = curl_exec($ch); 

list($headers, $response) = explode("\r\n\r\n", $resp, 2); 
// $headers now has a string of the HTTP headers 
// $response is the body of the HTTP response 

$headers = explode("\n", $headers); 
foreach($headers as $header) { 
    if (stripos($header, 'Location:') !== false) { 
     echo "The location header is: '$header'"; 
    } 
} 

查看所有選項curl_setopt()