3

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);
Rich Armstrong
  • 33
  • 1
  • 1
  • 4
  • PHP warning, json_encode() expects parameter 2 to be integer from the Apache logs. And the server itself is erroring with status.code unknown (helpful i know) – Rich Armstrong Oct 25 '18 at 11:29

3 Answers3

6

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\r\n",
        '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

  • Sorry man I'm kind of a n00b at coding, I am getting an output from that of resource #id 3. How do i view the contents of Context? – Rich Armstrong Oct 25 '18 at 11:34
  • You can use **$result = file_get_contents( $url, false, $context );** **$response = json_decode( $result );** after _$context_ and access it via the **$response** – Daryn van Vreden Oct 25 '18 at 12:10
  • That appears to have worked Daryn. However i believe there is an issue with the format in which the Json is being parsed. Which is an error being thrown by a Java server so i am going to look more into that. Thank you for your help man – Rich Armstrong Oct 25 '18 at 12:47
2

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.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • Unforunately, this didn't work for me. But i will make sure to keep it in my code, thank you for the tip – Rich Armstrong Oct 25 '18 at 11:36
  • you must have more errors then, but this at least gets you the correct array. any new error messages? – delboy1978uk Oct 25 '18 at 11:37
  • 1
    Yeah I am now getting the json_decode() expects parameter 1 to be string, resource given in /var/www/etc – Rich Armstrong Oct 25 '18 at 11:43
  • where are you decoding? – delboy1978uk Oct 25 '18 at 11:44
  • Regarding your updates, I need to pass a JSON object, does that not need to be json_encoded? – Rich Armstrong Oct 25 '18 at 11:47
  • 1
    Aha! Following your updates i have stopped getting any errors in Apache! I am getting a server error saying "status.cause:com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input" - but i assume that is another question for another thread? Edit: I was decoding in the first example, not the second, apologies. – Rich Armstrong Oct 25 '18 at 11:48
  • certainly sounds like it! that's a 400 I'm guessing? Which means wrong data in your request. – delboy1978uk Oct 25 '18 at 11:49
  • Yeah there is a chance that my request is being formatted wrong, I'll have a chat with the Java people. Thank you so much for your help Delboy! I'd upvote you, but i'm too new to have an input.. Hahah – Rich Armstrong Oct 25 '18 at 11:50
  • Try first actually sending the json as an encoded string instead of the array like i suggested, you never know! – delboy1978uk Oct 25 '18 at 11:57
  • If I encode it i get HTTP 100 Continue, and then the same errors as if i dont. – Rich Armstrong Oct 25 '18 at 12:06
  • 100 means headers sent and awaiting the body. so maybe the array is correct, but you just aren't sending the correct params https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100 – delboy1978uk Oct 25 '18 at 12:21
  • 1
    Yeah i had a hunch that was the case. I did look up code 100 and saw it's nothing bad. I will continue looking into the Java errors. – Rich Armstrong Oct 25 '18 at 12:42
1

Have you tried:

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

$body = json_encode($body);

Maybe its the way your array is setup

Matt Jameson
  • 582
  • 3
  • 16