【发布时间】:2016-05-12 23:07:03
【问题描述】:
这是我的代码:
$url = 'http://99.99.99.999:12480/rs/AuthorizationSession';
$credentials = 'admin:system';
$base64 = base64_encode($credentials);
// Set header text.
$header = "Content-type: application/json\r\n" .
"Accept: application/json\r\n" .
"RESTAuthorization: " . $base64;
// Set data.
$postdata = http_build_query(
array(
"authorization" => $base64
)
);
// Set options - POST.
$opts = array("http" =>
array(
"method" => "POST",
"header" => $header,
"content" => $postdata
)
);
// Create context and execute.
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
我不断收到:file_get_contents(http://99.99.99.999:12480/rs/AuthorizationSession): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
显然我改变了IP。这适用于 GET 请求,但不适用于 POST。
我想我的标题或帖子数据一定有问题,但我想不通。
想法?
【问题讨论】:
标签: php rest post curl file-get-contents