在PHP中向REST API发送JSON POST请求

3
我需要使用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);

这段文本涉及到IT技术,其中提到了HTTPful资源和cURL。我建议使用HTTPful资源进行操作,因为它更易于使用和理解。

$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显示了什么? - delboy1978uk
PHP警告,json_encode()期望第二个参数为整数,来自Apache日志。而服务器本身出现了状态码未知的错误(我知道这并不是很有帮助)。 - Rich Armstrong
3个回答

7

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

对我有效的解决方案是这样的:

$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 );

类似的答案可以在这里找到。


抱歉,我在编程方面有点新手,我从资源#id 3中得到了一个输出。如何查看上下文的内容? - Rich Armstrong
你可以使用以下代码:$result = file_get_contents($url, false, $context); $response = json_decode($result);$context 后面添加上述代码,然后通过 $response 访问它。 - Daryn van Vreden
那看起来已经成功了,Daryn。然而,我认为Json解析的格式存在问题,这是Java服务器抛出的错误,所以我会进一步研究。感谢你的帮助。 - Rich Armstrong

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

要更好地理解,请阅读文档

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

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

不要在提交字段后将其JSON编码,它需要一个数组。

不幸的是,这对我没有起作用。但我会确保将其保留在我的代码中,感谢您的提示。 - Rich Armstrong
1
是的,我现在得到了json_decode()期望参数1为字符串,但实际给出的是资源,在/var/www/etc中。 - Rich Armstrong
关于您的更新,我需要传递一个JSON对象,这不需要进行json编码吗? - Rich Armstrong
1
啊哈!根据您的更新,我在Apache中不再收到任何错误!但是我现在遇到了一个服务器错误,显示“status.cause:com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input”,但我认为这是另一个线程的问题,需要单独讨论?编辑:对不起,在第一个示例中我进行了解码,而不是第二个示例。 - Rich Armstrong
1
是的,我有一种预感就是这样。我查了一下100代码,发现它并不是什么坏东西。我会继续研究Java错误。 - Rich Armstrong
显示剩余7条评论

1
你尝试过吗:

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

$body = json_encode($body);

也许是您的数组设置方式导致了这个问题。

很遗憾,这两个都没有起作用。不过还是谢谢你的建议。 - Rich Armstrong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接