我有下面的腳本,想知道爲什麼我收到錯誤:PHP:未能打開流:HTTP請求失敗
failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required
Base64編碼值的格式爲username:password
和是正確的。 API文檔給的例子爲:
Authorization: Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==
例如,如果用戶代理使用阿拉丁作爲用戶名和芝麻開門作爲密碼,則該字段被形成爲上面在HTTP報頭
<?php
$postData = array(
'primaryContact' => array('id' => 1),
'subject' => 'Something not working'
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\r\nContent-Type: application/json",
'content' => json_encode($postData)
)
));
// Send the request
$response = file_get_contents('http://127.0.0.1/', FALSE, $context);
// Check for errors
if($response === FALSE){
die('Error');
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData;
?>
任何想法?
什麼是你想連接?它應該留在本地主機上嗎? – Hespen