Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP HTTP post-400错误请求_Php_Http_Post_Http Status Code 400 - Fatal编程技术网

PHP HTTP post-400错误请求

PHP HTTP post-400错误请求,php,http,post,http-status-code-400,Php,Http,Post,Http Status Code 400,我不是一个PHP程序员,但我必须编写一个PHP脚本,将HTTP POST请求发送到API。这篇文章还包括一些JSON内容(我已经用jsonlint.com验证了这些内容的格式是否正确)。我不断收到“400个错误请求”,所以我假设我有一些格式不正确的东西 $json = "<JSON markup goes here>"; $options = array( 'http' => array ( 'method' => 'POST',

我不是一个PHP程序员,但我必须编写一个PHP脚本,将HTTP POST请求发送到API。这篇文章还包括一些JSON内容(我已经用jsonlint.com验证了这些内容的格式是否正确)。我不断收到“400个错误请求”,所以我假设我有一些格式不正确的东西

$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 );
$json=”“;
$options=array(
“http'=>数组
(
'方法'=>'发布',
'content'=>$json,
'header'=>“内容类型:application/json\r\n”。
“接受:应用程序/json\r\n”。
“Api用户:\r\n”。
“Api密钥:\r\n”
)
);
$url=”https://url/paths";
$context=stream\u context\u create($options);
$result=file\u get\u contents($url,false,$context);
$response=json_decode($result);
我已经搜索了这个网站,大多数的例子似乎与我正在做的匹配…所以我不知道问题是什么。我注意到的一个区别(与示例相比)是我的帖子是发布到HTTPS url…但从我所读到的来看,这并没有多大区别。如果你们能提供任何帮助,我们将不胜感激

我没有看到任何“主机”标题。不包括“主机”可能会导致错误的请求

例如:

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

{
    "id" : "value"
}
此外,文档中的所有示例都使用
fopen
而不是
file\u get\u contents
。看

编辑:以下是一个示例

$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";
$options=array(
“http'=>数组
(
'方法'=>'发布',
'content'=>$json,
'头'=>“主机:http://localhost:8080\r\n”。
“内容类型:application/json\r\n”。
“接受:应用程序/json\r\n”。
“Api用户:\r\n”。
“Api密钥:\r\n”
)
);
$url=”http://localhost:8080/api/myService";

您可能需要使用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>';

?>

像这样尝试——不要将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"
    )
);
$json=array(“键”=>“值”);//你明白了吧?
$options=array(
“http'=>数组
(
'方法'=>'发布',
'content'=>json_encode($json),
'header'=>“内容类型:application/json\r\n”。
“接受:应用程序/json\r\n”。
“Api用户:\r\n”。
“Api密钥:\r\n”
)
);

您连接的api是什么?通常,他们会提供有关返回状态代码原因的文档。我看不到任何“主机”标题。遗憾的是,API文档仅说明“由于语法错误,服务器无法理解请求。”对于他们的400个错误请求:这就是为什么我认为这是我在构建请求时做错的事情。错误400可能只是API的正常响应。比如Mailchimp API,当一个人试图将已经存在的用户添加到邮件列表中时,就会给出这样的结果。下面是一个示例:{“内容”:{“模板ID”:“123456”},“更新配置文件”:true,“收件人”:[{“电子邮件”:“jerry”。garcia@gmail.com,“id”:“杰瑞。garcia@gmail.com“,”个人资料“:{“userAttrs”:[{“lastName”:“Garcia”},{“firstName”:“Jerry”},{“出生日期”:“1942-08-01”},},”,”订阅“{“add”:[“班卓琴爱好者”,“9指俱乐部”]}”如果你能用你的方法帮我格式化JSON,那就太棒了。我该如何在$options数组中定义它?我假设它只是“主持人:”?主机是我的服务器还是他们的服务器(API所在的服务器)?我尝试了这种方法,但在发送信息时,它似乎无法识别API用户名和密钥。有什么想法吗?不确定-可能尝试删除一些curl_setopt,例如CURLOPT_USERPWD,特别是因为您在$options arrayDownvoted中提供了这些信息,因为使用curl不会改变错误,而且比使用纯PHP更复杂。