【问题标题】:Sending a JSON POST request to REST API in PHP在 PHP 中向 REST API 发送 JSON POST 请求
【发布时间】:2019-03-29 23:48:35
【问题描述】:

我需要使用 JSON 对象作为主体发出 POST 请求。这两种方法都给我 HTTP 500 服务器错误。我的代码有什么明显的错误吗?要温柔... 我尝试了几种方法,包括

$checkfor = ("'serverId':'Server','featureId':'Feature','propertyId':'Property'");
    $checkforJson = json_encode($checkfor);
    $uri = "http://localhost:8080/v1/properties";
    $response = \Httpful\Request::post($uri)
    ->method(Request::post)
    ->withoutStrictSsl()
    ->expectsJson()
    ->body($checkforJson)
    ->send();
    pre($response);

使用 HTTPful 资源。我尝试过使用 cURL

$service_url = "http://localhost:8080/v1/properties";

   // Initialize the cURL
   $ch = curl_init($service_url);

   // Set service authentication

   // Composing the HTTP headers     
   $body = array();
   $body[] = '"serverId" : "Server"';
   $body[] = '"featureId" : "Feature"';
   $body[] = '"propertyId" : "Property"';
   $body = json_encode($body);

   $headers = array();
   $headers[] = 'Accept: application/xml';
   $headers[] = 'Content-Type: application/xml; charset=UTF-8';

   // Set the cURL options
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
   curl_setopt($ch, CURLOPT_POST, TRUE);
   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_HEADER, TRUE);
   curl_setopt($ch, CURLINFO_HEADER_OUT, true);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

   curl_setopt($ch, CURLOPT_TIMEOUT, 15);

   // Execute the cURL
   $data = curl_exec($ch);

   // Print the result
   pre($data);

【问题讨论】:

  • 太棒了。 500 个错误通常会告诉你代码哪里出错了。你的 error_log 说什么?
  • PHP 警告,json_encode() 期望参数 2 是 Apache 日志中的整数。并且服务器本身因 status.code unknown 出错(我知道有帮助)

标签: php grpc


【解决方案1】:

不久前我也遇到过类似的问题。

对我有用的解决方案是:

$url = 'http://yourURL.com/api';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
        'http' => array(
        'header'  => "Content-type: application/json\r\n",
        'method'  => 'POST',
        'content' => json_encode($data),
    )
);

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

可以找到类似的答案HERE

【讨论】:

  • 对不起,我在编码方面有点n00b,我从资源#id 3 得到输出。我如何查看上下文的内容?
  • 你可以使用 $result = file_get_contents( $url, false, $context ); $response = json_decode( $result ); $context 并通过 $response 访问它
  • 这似乎对达林有效。但是我认为解析 Json 的格式存在问题。这是Java服务器引发的错误,所以我将对此进行更多研究。谢谢你的帮助男人
【解决方案2】:

您的 json_encode 需要一个 数组

应该是这样的

<?php

$checkfor = ([
    'serverId'=>'Server',
    'featureId'=>'Feature',
    'propertyId'=>'Property'
]);

$checkforJson = json_encode($checkfor);
var_dump($checkforJson); // this will now work

https://3v4l.org/RG5Zv

为了更好地理解,请阅读 doc

更新我还注意到 curl 脚本,您的数组需要再次修复

 $body['serverId'] = 'Server';

之后不要对 post 字段进行 json 编码,它需要一个数组。

【讨论】:

  • 不幸的是,这对我不起作用。但我会确保将其保留在我的代码中,谢谢您的提示
  • 那么你肯定有更多的错误,但这至少能让你得到正确的数组。任何新的错误信息?
  • 是的,我现在得到的 json_decode() 期望参数 1 是字符串,在 /var/www/etc 中给出的资源
  • 啊哈!在您的更新之后,我已经停止在 Apache 中收到任何错误!我收到一个服务器错误消息“status.cause:com.fasterxml.jackson.databind.exc.MismatchedInputException:由于输入结束没有要映射的内容” - 但我认为这是另一个线程的另一个问题?编辑:我在第一个例子中解码,而不是第二个,道歉。
  • 是的,我有这样的预感。我确实查找了代码 100,发现它没什么坏处。我将继续调查 Java 错误。
【解决方案3】:

你试过了吗:

$body = array(
  "serverId" => "Server",
  "featureId" => "Feature",
  "propertyId" => "Property",
);

$body = json_encode($body);

也许你的阵列是这样设置的

【讨论】:

  • 不幸的是,这些都不起作用。谢谢你的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多