

It is possible to see what kind of request was made to a PHP script acting as a server by inspecting the $_SERVER value, which indicates which verb was used in the request. These requests shouldn’t be repeated or bookmarked, which is why your browser warns you when it is resubmitting data. In contrast to GET requests, a POST request is one that does cause change on the server that handles the request. For example, a GET request is used when filling in a search web form, which should always use GET. URLs used with GET can be bookmarked, they can be called as many times as needed, and the request should not affect change to the data it accesses. $data = array("name" => "Alex", "email" => = curl_init($url) Ĭurl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)) Ĭurl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')) Ĭurl_setopt($ch, CURLOPT_RETURNTRANSFER, true) Also, set a Content-Type header, which indicates to the server what format the body data is in. To make a POST request, set the CURLOPT_POST option to 1, and pass the data you want to send as an array to the CURLOPT_POSTFIELDS option.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) In most cases, this option should be used to capture the response rather than letting PHP echo it as it happens. The CURLOPT_RETURNTRANSFER option is to true, which causes Curl to return the results of the HTTP request rather than output them. Set the CURLOPT_URL option with the $url variable passed into the function: curl_setopt($ch, CURLOPT_URL, $url) The curl_setopt() function is used to set many different options on the Curl. Execute and fetch data from server or send data to server.Using curl in PHP is very simple process: You can use Curl to make all kinds of HTTP requests, including sending custom headers, sending body data, and using different verbs to make your request. It allows you to connect with other URLs and use their responses in your code.
