【问题标题】:PHP CURL Using POST Raw JSON Data使用 POST 原始 JSON 数据的 PHP CURL
【发布时间】:2017-12-06 13:22:51
【问题描述】:

我正在使用 PHP curl 进行发布,由于某种原因,我无法成功发布表单。

$ch = curl_init();
$headers = [
            'x-api-key: XXXXXX',
            'Content-Type: text/plain'
        ];
$postData = array (
    'data1: value1',
    'data2: value2'
);
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);           
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

当我尝试在帖子中使用相同的内容时,它可以工作,但不适用于 PHP。

var_dump($server_output) ==> string(67) ""require data1 and data2 or check the post parameters""
var_dump(curl_error($ch)) ==> string(0) ""

【问题讨论】:

  • var_dump($server_output)var_dump(curl_error($ch)) 给你什么?
  • 如果你发送text/plain$postData应该是一个字符串,而不是一个数组。
  • 我建议您检查此答案stackoverflow.com/a/13596901/1970395,因为它也适用于您的情况。你应该从你的数组中构建帖子字符串,而不是传递一个数组。
  • Json 在发送前对您的 $postData 进行编码。
  • 您确定 API 需要纯文本的参数吗?大多数使用 url 编码或 JSON。

标签: php json curl raw-post


【解决方案1】:

如果你想使用Content-type: application/jsonraw 数据,你的数据应该是json 格式

$ch = curl_init();
$headers  = [
            'x-api-key: XXXXXX',
            'Content-Type: text/plain'
        ];
$postData = [
    'data1' => 'value1',
    'data2' => 'value2'
];
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));           
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result     = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

【讨论】:

  • 块数组模式是解决问题的关键。
  • 为什么你的答案是纯文本/纯文本?
【解决方案2】:

如果你想要一个经过安排和解释的格式,请参见下面的代码。

// Set The API URL
$url = 'http://www.example.com/api';

// Create a new cURL resource
$ch = curl_init($url);

// Setup request to send json via POST`
$payload = json_encode(array(
    'data1' => 'value1',
    'data2' => 'value2'
   )
);

// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-api-key: XXXXXX', 'Content-Type: application/json'));

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the POST request
$result = curl_exec($ch);

// Get the POST request header status
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// If header status is not Created or not OK, return error message
if ( $status !== 201 || $status !== 200 ) {
   die("Error: call to URL $url failed with status $status, response $result, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
}

// Close cURL resource
curl_close($ch);

// if you need to process the response from the API further
$response = json_decode($result, true);

我希望这对某人有所帮助

【讨论】:

  • 检查状态应该是if (!$status || !($status == 201 || $status == 200)) {
  • 精彩观察@NickWeavers
  • 为什么两个答案都将 Content-Type 标头设置为 text/plain?
  • @danronmoon 感谢您的观察,我刚刚更新它以反映'application/json'
猜你喜欢
  • 2012-10-17
  • 2019-01-19
  • 2016-12-24
  • 1970-01-01
  • 2019-10-18
  • 2018-03-07
  • 2021-05-05
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多