我想使用file_get_contents“发布”到一个url并获得auth令牌。我遇到的问题是它返回一个错误。发送请求file_get_content not working
Message: file_get_contents(something): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required.
我不知道是什么原因造成这一点,但需要帮助。这是功能。
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("$username:$password"),
'method' => 'POST'
)
));
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
我试过用POSTMAN,它的工作原理,所以唯一的问题是,为什么它没有与file_get_contents一起工作。
一些东西必须与你的'$ context'混淆,并且'Content-Length'标题丢失。 –
@ patryk-uszynski如何添加Content-Length? –
'file_get_content'有一些限制。试用CURL –