PHP HTTP post - 400 Bad Request

时间:2014-10-24 19:42:25

标签: php http post http-status-code-400

我不是一个交易的PHP程序员,但我必须编写一个PHP脚本代码,该脚本将向API发送HTTP POST请求。这篇文章还包括一些JSON内容(我已经验证过使用jsonlint.com正确格式化了)。我不断得到" 400 Bad Request"所以我假设我的格式不正确。

$json = "<JSON markup goes here>";

$options = array(
    'http' => array
    (
        'method'  => 'POST',
        'content' => $json,
        'header'  =>    "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n" .
                "Api-User: <API USER GOES HERE>\r\n" .
                "Api-Key: <API KEY GOES HERE>\r\n"
    )
);

$url = "https://url/paths";

$context  = stream_context_create( $options );
$result   = file_get_contents( $url, false, $context );
$response = json_decode( $result );

我搜索了这个网站,大多数例子都与我正在做的事情相符......所以我对这个问题是什么感到茫然。我注意到的一个区别(与示例相比)是我发布到HTTPS网址...但是从我所读过的内容来看,这并没有太大的区别。我们非常感谢您提供的任何帮助!

3 个答案:

答案 0 :(得分:1)

尝试这样 - 不要将json作为字符串,使用PHP:

$json = array("keys" => "values"); // You get the idea, right?  

$options = array(
    'http' => array
    (
        'method'  => 'POST',
        'content' => json_encode($json),
        'header'  =>    "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n" .
                "Api-User: <API USER GOES HERE>\r\n" .
                "Api-Key: <API KEY GOES HERE>\r\n"
    )
);

答案 1 :(得分:0)

我没有看到任何&#34;主持人&#34;头。不包括&#34;主持人&#34;可能导致错误请求。

例如:

POST http://localhost:8080/api/myService HTTP/1.1
Host: http://localhost:8080
Content-Type: application/json

{
    "id" : "value"
}

此外,文档中的所有示例都使用fopen而不是file_get_contents。见http://php.net/manual/en/function.stream-context-create.php

编辑:以下是一个例子......

$options = array(
    'http' => array
    (
        'method'  => 'POST',
        'content' => $json,
        'header'  => "Host: http://localhost:8080\r\n" .
                "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n" .
                "Api-User: <API USER GOES HERE>\r\n" .
                "Api-Key: <API KEY GOES HERE>\r\n"
    )
);

$url = "http://localhost:8080/api/myService";

答案 2 :(得分:-1)

您可能需要使用curl:

<?php

function getCurl($url, $post) {

    $appName = 'My Application';
    $apiUser = [Username Goes Here];
    $apiKey = [API Key Goes Here];

    $baseUrl = "https://url/";
    $credentials = $apiUser:$apiKey;
    $typeHeader = "Content-Type: application/json";
    $helloHeader = "User-Agent: $appName ($appContact)";

    $url = $baseUrl.$url;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, $credentials);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($typeHeader, $helloHeader));
    return $ch;
}

$options = array(
    'http' => array
    (
        'method'  => 'POST',
        'content' => $json,
        'header'  =>    "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n" .
                "Api-User: <API USER GOES HERE>\r\n" .
                "Api-Key: <API KEY GOES HERE>\r\n"
    )
);

$curl = getCurl("paths", $options);
echo curl_exec($curl).'</br>';
echo curl_errno($curl).'</br>';
echo curl_error($curl).'</br>';

?>