0

I have created a JSON object using php functionality json_encode

The Object is formulated this way:

$object_array[]=array('value1' => $value1, 'value2' => $value2);

$json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES);

I need to post the $json_output to a URL using the 'cUrl' functionality of PHP.

Can anyone suggest something based on the above code ?

Tony Borcello
  • 11
  • 1
  • 4
  • You can use url_encode – merdincz Apr 18 '16 at 12:55
  • Please refer to: http://stackoverflow.com/questions/5576619/php-redirect-with-post-data – Dan H Apr 18 '16 at 12:58
  • I need to post the Object to another server which will not use PHP to decode. Is it possible to use something to just send the exact object ? something similar to `$.post("url", $json_output)` ? How can I integrate php and jquery/ajax ? – Tony Borcello Apr 18 '16 at 13:24
  • maybe interesting? [JavaScript URL Decode function](http://stackoverflow.com/questions/4292914/javascript-url-decode-function). It seems that PHP 'rawurlencode' may be useful? – Ryan Vincent Apr 18 '16 at 13:37
  • @RyanVincent But my issue is in some way I need to pass the JSON object onto another server not from where I create it so in some way I need to define a URL – Tony Borcello Apr 18 '16 at 13:38
  • @RyanVincent is it possible to explain directly to you in a private chat or something and then update the question to reflect the correct answer ? – Tony Borcello Apr 18 '16 at 13:43
  • @RyanVincent let's go a step before, I am still stuck in how to post the data from server A to server B. I have created the JSON object on Server A using PHP and storing into a variable. Now the first next step is how to pass the content of that array to Server B via a post – Tony Borcello Apr 18 '16 at 13:50
  • ok, looked at 'cUrl'? It is a universal http client that exists on every platform. maybe interesting? https://curl.haxx.se/docs/httpscripting.html. As usual, PHP has access to it: Tutorial: http://www.binarytides.com/php-curl-tutorial-beginners/. There are other, easier to use ones, but 'cUrl' is tried and tested. – Ryan Vincent Apr 18 '16 at 13:57
  • @RyanVincent many thanks for the info. Are you able to give me a small sample code based on the code I provided on how to create the submission using 'cUrl' ? – Tony Borcello Apr 18 '16 at 14:07
  • glad it got sorted out. :) – Ryan Vincent Apr 19 '16 at 17:30

2 Answers2

2

Post json objectusing curl.

$data = array('value1' => $value1, 'value2' => $value2);                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');   // where to post                                                                   
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);
Oyeme
  • 11,088
  • 4
  • 42
  • 65
  • Many thanks for your answer, I am getting the following error when I execute: `PHP Fatal error: Call to undefined function curl_init()` any idea on how to get rid of it ? – Tony Borcello Apr 18 '16 at 14:35
  • @TonyBorcello you need to install php curl libary. sudo apt-get install php5-curl sudo service apache2 restart – Oyeme Apr 18 '16 at 14:36
  • @TonyBorcello how to install curl http://askubuntu.com/questions/9293/how-do-i-install-curl-in-php5 – Oyeme Apr 18 '16 at 14:38
0

urlencode() is the best method to post URL in coded form...

$json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES);
$url = 'www.example.com?url='.urlencode($json_output);

And you will get your array after urldecode() of url parameter...

$json = urldecode($_GET['url']);
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20