在PHP中向RESTAPI发送JSON POST请求

在PHP中向RESTAPI发送JSON POST请求,php,grpc,Php,Grpc,我需要使用JSON对象作为主体发出POST请求。这两种方法都给了我HTTP 500服务器错误。我的代码有什么明显的错误吗?温柔点。。。 我试过几种方法,包括 $checkfor = ("'serverId':'Server','featureId':'Feature','propertyId':'Property'"); $checkforJson = json_encode($checkfor); $uri = "http://localhost:8080/v1/propert

我需要使用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);
您是否尝试过:

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

$body = json_encode($body);

也许这就是数组的设置方式

您的json_编码需要一个数组

应该是这样的

<?php

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

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

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

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

一个对我有效的解决方案是:

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

类似的答案也可以找到

太好了。500个错误通常会准确地告诉您代码出错的地方。错误日志会说什么?PHP警告,json_encode()希望参数2是Apache日志中的整数。服务器本身出现错误,status.code未知(我知道很有帮助),不幸的是,这两个都不起作用。谢谢你的建议,抱歉,伙计,我在编码方面有点不在行,我从资源id 3中得到了一个输出。如何查看上下文的内容?可以使用$result=file\u get\u contents($url,false,$Context)$response=json_decode($result);在$context之后,通过$responseThat访问它,Daryn似乎已经工作了。然而,我认为Json解析的格式存在问题。这是一个Java服务器抛出的错误,所以我将对此进行更多的研究。谢谢你的手动帮助,这对我没用。但是我会确保在我的代码中保留它,谢谢你的提示,你肯定有更多的错误,但这至少让你得到了正确的数组。有新的错误消息吗?是的,我现在得到了json_decode()期望参数1是字符串,资源在/var/www/etcAha中给出!在您的更新之后,我已经停止在Apache中获取任何错误!我收到一个服务器错误,上面写着“status.cause:com.fasterxml.jackson.databind.exc.MismatchedInputException:由于输入结束,没有要映射的内容”-但我假设这是另一个线程的另一个问题?编辑:我在第一个例子中解码,而不是第二个,道歉。是的,我有预感就是这样。我查了一下代码100,发现没什么不好的。我将继续研究Java错误。
$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 );