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