1

I need to post json object with http post request and to handle responses.

json object :

{
   "my_json" : "12345" 
}

I wrote somethinh like this,but this don't work.

$url = "http://localhost/my_json.json";

$json_Data = file_get_contents($url,0,null,null);

print_r($json_Data);

And it doesn't print anything.

Help please.

8
  • Are you sure that your web server is correctly serving /my_json.json ? Commented Dec 9, 2010 at 12:23
  • Is allow_url_fopen enabled in your php.ini file to enable requesting of URLS through file_get_contents() etc? See php.net/manual/en/… Commented Dec 9, 2010 at 12:24
  • allow_url_fopen is enabled.How to make sure that my web server is correctly serving ? what do you mean? Commented Dec 9, 2010 at 12:27
  • Point your browser to localhost/my_json.json and see if you receive the file. Also, if you are trying to POST (in the sense of HTTP POST), you might be better off with curl. Commented Dec 9, 2010 at 12:29
  • i second what Ramon says, cURL might be better Commented Dec 9, 2010 at 12:31

4 Answers 4

2

Client:

<?php
$data = array('foo' => 'bar', 'red' => 'blue');


$ch = curl_init();
$post_values = array( 'json_data' => json_encode( $data ) );
curl_setopt($ch, CURLOPT_URL, 'http://localhost/server.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
$data = curl_exec($ch);
if(!curl_errno($ch))
{
  echo 'Received raw data' . $data;
}
curl_close($ch);
?>

Server (server.php):

<?php
$data = json_decode( $_POST['json_data'] );
// ... do something ...
header('Content-type: text/json');
print json_encode($response);
?>
Sign up to request clarification or add additional context in comments.

8 Comments

Ok,but I create dynamicly json object.Then point that object to another url another_url.'/my_json.json' and nothing retrive :(
Don't take me wrong, but what are you exactly trying to do? According to your question "I need to POST a json object with a HTTP POST request and to HANDLE RESPONSES.".
No,it is ok,maybe problem is that I send json object to https,and I need to retrive object from https
I need in php file to post object and to retreive it from https
This is the way that I create json obj $jsob = array(); $jsob['my_json'] = "12345"; $some_data = json_encode($jsob);
|
0
$url = "http://localhost/my_json.json";

$json_Data = file_get_contents($url,0,null,null);

$new = json_decode($json_Data);

print_r($new);

I think that might do it

1 Comment

@user303832: Are you sure you have correct setup on your server? This code is 100% valid.
0

Try this:

$jsonFile = 'http://localhost/my_json.json';
$jsonData = file_get_contents($jsonFile);
$phpData = json_decode($jsonData);
print_r($phpData);

1 Comment

Are you sure you can access localhost/my_json.json using your web browser?
0

The problem may be from the file_get_contents extraneous arguments :

  • The 2d arg should be a boolean and is optional (default value is false)
  • The 3rd arg is ok
  • The 4th should be an integer, is optional (default value is -1)

So you should try $json_Data = file_get_contents($url);

Furthermore to view the data in your browser you should try with header('Content-type: text/plain'); just before outputting with print_r() so that no processing will be made by your browser

To be sure there is really nothing sent to your browser, you may also try FireFox + FireBug to see HTTP replies...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.