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 ?

10
  • You can use url_encode Commented Apr 18, 2016 at 12:55
  • Please refer to: stackoverflow.com/questions/5576619/php-redirect-with-post-data Commented Apr 18, 2016 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 ? Commented Apr 18, 2016 at 13:24
  • maybe interesting? JavaScript URL Decode function. It seems that PHP 'rawurlencode' may be useful? Commented Apr 18, 2016 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 Commented Apr 18, 2016 at 13:38

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
@TonyBorcello you need to install php curl libary. sudo apt-get install php5-curl sudo service apache2 restart
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']);

4 Comments

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 ?
then you can use JSON.stringify(json_array) that converts object to string and you can send it easily and use JSON.parse(jsonString); to decode it..
But how can I pass the PHP object to the jquery ?
In your script file var json_array = <?php echo $json_output; ?>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.