donglun2024
2018-10-25 11:18
浏览 256
已采纳

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

I need to make a POST request using a JSON object as the body. Both of these methods are giving me HTTP 500 server errors. Is there anything glaringly wrong with my code? Be gentle... I've tried several methods including

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

Which uses the HTTPful resource. And I have tried using 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);

图片转代码服务由CSDN问答提供 功能建议

我需要使用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)
  - >方法(Request :: post)
  - > withoutStrictSsl()
  - > expectedJson()
  - > body($ checkforJson)
  - > send()  ; 
 pre($ response); 
   
 
 

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

  $ service_url =“http:// localhost:8080 / v1 / properties”; 
 
 //初始化cURL 
 $ ch  = curl_init($ service_url); 
 
 //设置服务身份验证
 
 //编写HTTP头
 $ body = array(); 
 $ body [] ='“serverId”:“服务器”  '; 
 $ body [] ='“featureId”:“Feature”'; 
 $ body [] ='“propertyId”:“Property”'; 
 $ body = json_encode($ body); 
 \  n $ headers = array(); 
 $ headers [] ='Accept:application / xml'; 
 $ headers [] ='Content-Type:application / xml;  charset = UTF-8'; 
 
 //设置cURL选项
 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); 
 
 //执行cURL 
 $ data = curl_exec($ ch); 
 
 //打印结果
 pre(  $ data); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtufl26404 2018-10-25 11:25
    已采纳

    Your json_encode requires an array.

    It should look like this

    <?php
    
    $checkfor = ([
        'serverId'=>'Server',
        'featureId'=>'Feature',
        'propertyId'=>'Property'
    ]);
    
    $checkforJson = json_encode($checkfor);
    var_dump($checkforJson); // this will now work
    

    https://3v4l.org/RG5Zv

    For better understanding read doc

    UPDATE I also notice on the curl script, your array needs fixed again

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

    and dont json encode the post fields afterwards, it takes an array.

    打赏 评论
  • dongyun6003 2018-10-25 11:23

    Have you tried:

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

    Maybe its the way your array is setup

    打赏 评论
  • duanlinma5885 2018-10-25 11:25

    I had similar issues a while back.

    A solution that worked for me was this:

    $url = 'http://yourURL.com/api';
    $data = array('field1' => 'value', 'field2' => 'value');
    $options = array(
            'http' => array(
            'header'  => "Content-type: application/json
    ",
            'method'  => 'POST',
            'content' => json_encode($data),
        )
    );
    
    $context  = stream_context_create($options);
    $result = file_get_contents( $url, false, $context );
    $response = json_decode( $result );
    

    Similar answers can be found HERE

    打赏 评论

相关推荐 更多相似问题