Skip to main content

Posts

Showing posts with the label cURL

How to POST and Receive JSON Data using PHP cURL

When you working with web services and APIs, sending JSON data via POST request is the most required functionality. PHP cURL makes it easy to POST JSON data to URL. In this tutorial, we will show you how to POST JSON data using PHP cURL and get JSON data in PHP. Send JSON data via POST with PHP cURL The following example makes an HTTP POST request and send the JSON data to URL with cURL in PHP. Specify the URL ( $url ) where the JSON data need to be sent. Initiate new cURL resource using curl_init() . Setup data in PHP array and encode into a JSON string using json_encode() . Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option. Set the Content-Type of request to application/json using CURLOPT_HTTPHEADER option. Return response as a string instead of outputting it using CURLOPT_RETURNTRANSFER option. Finally, curl_exec() function is used to execute the POST request. //API URL $url = 'http://www.example.com/api'; //create a new cURL resource $ch = curl_ini...

PHP Post data using cURL

In this article am going to explain you how to make HTTP POST using PHP cURL. Some places you may need to post data to remote server where the data will insert to database or process and send you the response. Mostly APIs will use these methods. Apart from API we require it to post the data to remote server from local or for cross domain request. User will make a conventional form submission and we need to get those values and create an cURL request. In this method we send a set of (array) data and remote script will insert to database (MYSQL Insert) or some other data process. PHP cURL Post Now we are going to call the set of php cURL function to post data, //Initialize cURL $ch = curl_init(); //Set location to post data using cURL curl_setopt($ch, CURLOPT_URL,"http://www.your-website.com/yourScript.php"); //Enable POST method curl_setopt($ch, CURLOPT_POST, 1); //Create a array of data curl_setopt($ch, CURLOPT_POSTFIELDS, ...