2013-04-25 171 views
1

我熟悉使用PHP的XML飼料從顯示的項目和,但我剛開始工作的一個API要求我包括HTTP請求頭......HTTP請求頭

Feed-Auth => myPassword 

我如何包括一個HTTP請求頭?

回答

1
header("Feed-Auth:".$myPassword); 

如果你打使用像curlsoap的API,他們必須設置頁眉他們自己的機制。

捲曲:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Feed-Auth:'.$myPassword)); 

SOAP

$header = array('Feed-Auth'=>$myPassword); 
    $soapHeader = new SoapHeader('NAMESPACE','Header',$header,false); 
    $client->__setSoapHeaders($oapHeader); 
+0

不幸的是,SimpleXML的不具備這樣的機制。 – 2013-04-25 18:27:16

0

你將不得不加載文檔以另一種方式,並使用simplexml_load_string()

$body = file_get_contents($url, false, stream_context_create(array(
    'http' => array(
     'header' => 'Feed-Auth: myPassword', 
    ), 
))); 

$doc = simplexml_load_string($body); 
+0

所以這是我現在的代碼.. <?php $ theurl ='http://www.thesite.co.uk/feedurl.xml'; $ xml = simplexml_load_file($ theurl); $ result = $ xml-> xpath(「/ Search/Match /」); foreach($ result as $ item){ echo $ item-> SubItem; }?> 這可以很容易地適應? – user2227359 2013-04-25 16:27:53

+0

傑克,在你上面的例子中... $ url ...會不會是供稿網址? – user2227359 2013-04-25 16:39:59

+0

@user是的,沒錯。 – 2013-04-25 17:25:01