Send JSON by POST with php to a webservice

6

Situation:

I have a script with php in which I am receiving some data, with that data I create a JSON and that JSON I need to send it by POST with php5 (pure, no framework) to a web service in an online site.

I found that it can be done with php5-curl, but since it's a hostinig service that the site is on, I can not install those packages.

My question:

Is there a way to do that JSON sending only using php? and if so, could you tell me how to do it or where or how to look for it? because I only encounter as a do but with php5-curl.

Thank you very much, but I was referring a little bit more to this, but still thanks for answering:)

$postData = array(
    'kind' => 'blog',
    'blog' => '3',
    'title' => 'A new post',
    'content' => 'With <b>exciting</b> content...'
);

// Create the context for the request
$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => "Authorization: application/json\r\n".
            "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
));

// Send the request
$response = file_get_contents('http://www.example.com/blogger/blog/posts/', FALSE, $context);

// Check for errors
if($response === FALSE){
    die('Error');
}

Now the detail that I have is that it always enters the condition, that is to say that it always returns false, I'm not sure if it is the webservices that is returning that or does not even try to send the json, suggestions? Help please! : C

In this line is where I do not know what it is doing, if it does not go out or the web service returns that ... I'll go more to do not send data.

$response = file_get_contents('http://www.example.com/blogger/blog/posts/', FALSE, $context);

I gave a var_dump to $response and it returns me bool(false)

    
asked by Yasser Batas 27.09.2016 в 19:41
source

3 answers

0

In my case, in the last projects that I have worked, I wanted to return data by JSON from the backend in PHP. The normal thing is that they were results of a database or something like that.

For these cases, without using frameworks, I create an array with the data that I need to return and then I simply use

echo json_encode(miArray);

to return them to the site that requested them (often a request $.post of jQuery).

    
answered by 27.09.2016 в 20:20
0

the file_get_contents (...) function should be enclosed in a try ... catch to handle possible errors:

try {
  // Send the request
  $response = file_get_contents(...);
  // Check for errors
  if($response === false){
    die('Error');
  }
} catch (Exception $e) {
  // Handle exception
}
    
answered by 17.09.2017 в 00:25
0

Hello! I respond in order to serve as a reference to someone in the future. The best (and simplest) way to execute requests to web services rest with json is to use the library of Guzzle

With this you can perfectly handle any http request by any of the methods, with a simple header and body control, in addition to which the response object can be broken down to use it as best suits you.

According to the example in the question, the use would be like this:

$client = new \GuzzleHttp\Client();
     'kind' => 'blog',
     'blog' => '3',
     'title' => 'A new post',
     'content' => 'With <b>exciting</b> content...'
    $response = $client->post("http://www.example.com/blogger/blog/posts/", array(
        'body' => json_encode($payload)
    ))->getBody();

    $responseArray= json_decode($response, true);  // Si la rta es JSON 
    var_dump($responseArray); // acá ves lo que devuelve el web service.

Greetings.

    
answered by 08.11.2017 в 22:27