通过cURL POST PHP传递JSON

I've been trying to pass JSON though my web application using cURL - and I am a little stuck now.

Here is what I've tried :

Step 1:

I tried to post JSON using this

<?php 

  public function post(){

    $cars = array("Volvo", "BMW", "Toyota");
    $json = json_encode($cars);

    $ch = curl_init("http://localhost/api_v2/url?key=***");

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $json));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

  }

?>

Step 2:

I tried to receive JSON back using this

public function get(){

        $json = json_decode(Input::get('json'));
        dd($json); // null

    }

Result: - I keep getting null when I do dd($json);

Can someone please help me point out what did I do wrong ?


Detail :

  • I use PHP Framework : Laravel 4.0
  • I am positive that URL parameter is correct because I can go to it
  • I am also sure that the JSON is not broken because after added print("<h1> JSON </h1><pre>".print_r($json,true)."</pre><br><hr><br>"); I can see my JSON display fine.

  • See Image

As far as your debugging efforts, you should dump the response rather than json_decode(s) attempt to parse it.

So change this

public function get() {
    $json = json_decode(Input::get('json'));
    dd($json); // null
}

to this

public function get() {
    dd(Input::get('json'));
}

That should better help you track down the real problem, which most likely is that the server isn't responding with valid JSON.

Another option is to use json_last_error to see why the response was unparseable.

public function get() {
    $json = json_decode(Input::get('json'));

    // If the response was parseable, return it
    if($json !== null)
        return $json;

    // Determine if the response was a valid null or
    // why it was unparseable
    switch (json_last_error()) {
        // The server could respond with a valid null,
        // so go ahead and return it.
        case JSON_ERROR_NONE:
            return $json;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
            break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
            break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
            break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
            break;
        default:
            echo ' - Unknown error';
            break;
        }
}

Looks like you aren't returning the results of the cURL call.

public function post()
{
   // The first part of your original function is fine...

   $response = curl_exec($ch);
   curl_close($ch);

   // But you need to return the response!
   return $response;
}

You can't print stuff on the server side if your client is a script (ie: not the browser).

Everything you print on the server side will be returned to the client (the post() script).

That being said, your json should be present in the $response variable. You can output that. But it's not the best way to debug the api requests.

The easier way would be remove the dd() and write to a log file instead.