Php curl post content type
JSON is the most popular data format for exchanging data between a browser and a server. The JSON data format is mostly used in web services to interchange data through API. 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. Show
Send JSON data via POST with PHP cURLThe following example makes an HTTP POST request and send the JSON data to URL with cURL in PHP.
// API URL Receive JSON POST Data using PHPThe following example shows how you can get or fetch the JSON POST data using PHP.
$data = json_decode(file_get_contents('php://input'), true); Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request If you have any questions about this script, submit it to our QA community - Ask Question I was recently working on a project where I needed to integrate an external API using HTTP cURL requests. It was my first time doing this and I had a lot of problems figuring this out. I wrote this post so I can remember my cURL API calls for next time, and maybe it can help you as well. The API calls and functions I’m using in this post are all working examples on PHP -v 5.6.
PHP cURL BasicscURL stands for ‘Client URL Library’ and it allows you to connect and communicate with different types of servers with many different types of protocols (HTTP, https, FTP, proxy, cookies, …). More info about how cURL actually works can be found in the official PHP documentation. This article will provide more in-depth examples for integrating your applications. I’ve received a lot of responses on ‘how does cURL actually work’ and I get the feeling that people don’t know what’s going on in a cURL call. Before we start with the article and our cURL setup, I’ve added a simple example of a plain cURL request. The request will return the API response as a string.
Note that we stored our curl_exec() in a variable $output. This $output variable is still available in our program even after we closed it with curl_close(). So after we did our call and closed the connection, we can still access the result using our $output variable. Now that we understand the basics, let’s try to put this into a function we can reuse within our application. PHP cURL setupImplementing an external API into your project is probably going to take more than just one API call and from different pages in your project. This is why I’ve created a ‘simple’ PHP script that allows us to call this function, with a set of parameters, and a cURL request will be done. Make sure to put this code into a file or place that can be accessed by your entire app or website. (I’ve updated this function so we’ll be able to define the headers when we’re making the call. I’ve added a section for custom headers at the bottom!)
This is a basic setup for doing a cURL call and I’m using a I’m using PHP cURL GET requestThe most simple API call is the GET call, so let’s start with that! Our callAPI function expects 3 parameters: $method, $url and $data. We need to give those parameters to all our API calls, so for a cURL GET, we can just set $data on false because we are not passing any data with a GET call.
$get_data already returns all the data we want from the API in a json string. I’m using $response to convert the json string back to a usable PHP array. You can skip those steps if you want, this is my personal preference. I’m also using the extra $errors and $data arrays to store the actual data and errors. PHP cURL POST requestObviously, a POST request does require data. Make sure your json-data is correct, otherwise the request will keep returning errors. Although… If we receive errors from the API, that means our calls are working 😉 In my example I’m using the CakePHP syntax for setting up my json array, so don’t mind that.
Because we’re doing an API call with json data, I’m converting my PHP array to a json string with PHP cURL PUT requestThe PUT request is almost the same as the POST request. I had a hard time figuring out how to pass data into a PUT call. If we take a look at our callAPI() function, you see that I changed some things up between the PUT and the POST request. We can still use the same parameters in our callAPI() function as always.
PHP cURL DELETE requestThe delete request is very simple again. We can just hit the API url with the $id we want to remove and poof… it’s gone forever.
In the beginning we defined our callAPI function with preset headers. But what if we, for some reason, need to change the headers a bit for another call? We don’t want to write a whole new callAPI function just to edit some headers. Therefore, here’s an option on how to make the preset headers flexible:
There are 2 differences here from our first function. 1: We’ve added an
extra parameter in our function to define if we want to use a custom header or not. I put it to The second change is the if-statement when we’re setting the API headers. If we didn’t give in any headers when we make the call, it’s going to use our default headers instead of crashing. Now we’re ready to add custom headers with our call! In this example, I’m using search parameters to search for specific data before I’ll pull in all the data with the API. To make the search, I obviously need to be able to add my search query into my callAPI headers. Here’s my example:
This is just an example on how to add headers. My example is to get all rows where a rent was paid in the last 30 days. $one_month_ago is just a helper variable. The $rent_header is the actual header I want to add to my default headers. This needs to be a string!! When you’ve set the header, you can just do a regular api call and add your new header at the end. I didn’t need to use any other API call methods like patch or purge or anything like that. These you need to figure out yourself. If there’s some magic going on in this post I hope my examples can give you a better understanding. I recently wrote a part 2 for this post, that will talk about generating an AUTH-key (utoken) before we make our calls. Make sure to check it out here as well! How do you send a POST request on cURL?You can send a POST request with Curl by explicitly specifying the POST method with the -X POST command line parameter or passing data to Curl using the -d or --data command line parameter.
What is contentContent-Type - Everything curl. Content-Type. POSTing with curl's -d option will make it include a default header that looks like Content-Type: application/x-www-form-urlencoded . That is what your typical browser will use for a plain POST. Many receivers of POST data do not care about or check the Content-Type header.
What is Curlopt_returntransfer in cURL?Description ¶ curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.
What is Curlopt_postfields?CURLOPT_POSTFIELDS. The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.
|