php - HTTP 400 错误请求 - CURL PHP

标签 php api http curl http-status-code-400

我正在尝试使用 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;
}

最佳答案

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

关于php - HTTP 400 错误请求 - CURL PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21633264/

相关文章:

php - 如何检查我的服务器上的 query_cache_type 值

android - 如何将一个 Android 应用程序中的 API 公开给另一个应用程序

python - django api 用于在数据库中创建数据

http - 为什么我更改用户代理时被拒绝?

javascript - angularjs 无法使用 POST 与 php 通信

php - 如果 error_log 指令只是说 "error_log",PHP 错误记录到哪里?

php - 陷入 php foreach 循环 - 需要再次使用相同的键运行循环

php - 无法运行套接字服务器

Python Glassdoor API

http - 自定义 HTTP 授权 header