简体   繁体   中英

What is the best way to transfer data from database using curl

I have been reading a lot about php CURL and I still couldn't grasp the mechanism of it sending data, it seems that it is not possible to send php array or php object straight away from a database using curl, but it seems to be possible to send the data using JSON as curl return the data as strings, but I have read somewhere that it is possible to make the php array or object as a post data and send it through curl (kinda like ajax I guess). However I have not found the clean and proper tutorial or examples regarding this.

I hope that anyone that has enough knowledge about curl would share an example on how to properly send data, say from from mysql database using curl as an array / object / JSON. Hope it can be useful for anyone looking for ways to send data cross-domain using curl in the future.

Thank you in advance.

cURL makes HTTP requests. It can make an HTTP request to an HTTP server and it can get an HTTP response from an HTTP server.

It can't communicate with a database. It can communicate with an HTTP server that runs a server side program (which could be written in PHP) which gets data from a database, formats it in an HTTP response and sends that.

An HTTP request or response can include text. A PHP array is not text. A PHP object is not text. JSON is text. The json_encode function will convert a PHP array into a JSON text.

You can't send the data as array using cURL, So your best bet at this point is to send it using JSON. Which you can do the following way..

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://your-url-to-post-the-data');                                                                      
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);

You can also follow the instructions from POSTing JSON Data With PHP cURL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM