HTTP 400 错误请求 - CURL PHP

Posted

技术标签:

【中文标题】HTTP 400 错误请求 - CURL PHP【英文标题】:HTTP 400 Bad Request - CURL PHP 【发布时间】:2014-03-05 04:34:14 【问题描述】:

我正在尝试使用 CURL 发送 PUT 请求,但我收到 HTTP 400 Bad Request,我正在使用 CURLOPT_VERBOSE 进行调试,因此我收到:“错误: 格式错误的请求: 期望找到对象: \"email\"" 有谁知道这意味着什么/我该如何解决?

代码:

// Set the HTTP headers needed for this API call.

$http_headers = array(
//    "Authorization: Basic xxxxxxxxxxxxxxxxxx",
"Accept: application/json",
"Connection: close",                    // Disable Keep-Alive
"Expect:",                              // Disable "100 Continue" server response
"Content-Type: application/json"        // Content Type json
);

// URL Endpoint of the API to be called.
$ym_api_url = 'https://services.yesmail.com/enterprise/subscribers/718880';
$newsubscriber = array(
'email' => 'karina@email.com',
'firstName' => 'Karina',
'lastName' => 'McG',
'postCode' => 'BT93 EP3',
'prefersMobile' => '',
'emailFormat' => 'html'
);

$curl_hdl = curl_init();

$curl_options = array(
CURLOPT_VERBOSE => TRUE,               // Verbose mode for diagnostics
CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'),            // Verbose output to STDERR
CURLOPT_HEADER => TRUE,               // Include the header in the output.
CURLOPT_HTTPHEADER => $http_headers,  // HTTP headers to set
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,   // Use basic authentication.
CURLOPT_USERPWD => 'xxxxxxx:xxxxxxxx', //Username/Password
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, // Bitmask of protocols libcurl may use in this transfer
CURLOPT_RETURNTRANSFER => TRUE,       // Return result as return value of curl_exec()
CURLOPT_URL => $ym_api_url,           // URL to POST data to
);
curl_setopt_array($curl_hdl, $curl_options);

//Send array to URL
curl_setopt($curl_hdl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl_hdl, CURLOPT_POSTFIELDS, http_build_query($newsubscriber));

//SSL Bypass (!Temporary!)
curl_setopt($curl_hdl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_hdl, CURLOPT_SSL_VERIFYPEER, 0);

// Make the API call
$response = curl_exec($curl_hdl);

// Get the http response code
$http_response_code = curl_getinfo($curl_hdl, CURLINFO_HTTP_CODE);

// Echo out the Verbose Information
echo "Verbose information:\n<pre>", !rewind($verbose),  htmlspecialchars(stream_get_contents($verbose)), "</pre>\n";
curl_close($curl_hdl);

echo PHP_EOL . "INFO: HTTP Response Code was: " . $http_response_code . PHP_EOL;

if ( $response === false )

echo PHP_EOL . "ERROR: curl_exec() has failed." . PHP_EOL;

else

echo PHP_EOL . "INFO: Response Follows..." . PHP_EOL;
echo PHP_EOL . $response;

【问题讨论】:

您在标头中指定要发送 json,但我在您的代码中看不到任何 json。您的端点需要什么格式? 好的,但您没有以 json 或 xml 格式发送数据。你需要先转换它们。 好的,谢谢,我会这样做的! 【参考方案1】:

错误请求来自您尝试访问的服务,而不是来自 curl。因此,您需要确保以正确的格式向他们发送数据。

【讨论】:

以上是关于HTTP 400 错误请求 - CURL PHP的主要内容,如果未能解决你的问题,请参考以下文章

HTTP 400 错误

php curl 如果在循环中返回 400 Bad Request

http请求400错误

PHP CURL - 错误的请求发布 - 当 POSTFIELDS 非常大时

OAuth、PHP、Rest API 和 curl 给出 400 Bad Request

Python post 请求在请求库中引发 400 'Bad Request' 错误,但适用于 cURL