We use the $_POST global variables to read the data.įor example, create the http_build_query() function and create an array inside the function. The file_get_contents() function reads the content of the url into string. We use the stream_context_create() function to handle the streamed data. We can create an array to specify the http headers, methods and the contents. We can use the http_build_query() function to create query parameters to send in the POST request. We can send the POST request in PHP by using the functions like http_build_query(), stream_context_create() and file_get_contents() functions without using the CURL. Use the CURL-less Method Using the stream_context_create() and file_get_contents() Functions to Send a POST Request in PHP We can reuse the code supplying the different post data and the URL. It uses a user-defined function to send the request. This method is very much similar to the second method. We will show you an example to send the POST request in PHP using the CURL by creating a function. If it has not been installed, we should install it manually to send the server’s request using PHP. The CURL module is generally pre-installed with PHP. CURL is used to send the HTTP request to the webserver. We will show you a method to send the POST request in PHP using the CURL. We will introduce a method to send a POST request in PHP using the CURL-less method with the use of the http_build_query(), stream_context_create() and file_get_contents() functions. Create a User-Defined Function That Uses the CURL to Send the POST Request in PHP.Use the CURL to Send the POST Request in PHP.Use the CURL-less Method Using the stream_context_create() and file_get_contents() Functions to Send a POST Request in PHP.The file_get_contents() function is used to receive data in a more readable format.Use json_decode() function to decoded JSON data in PHP.The following example shows how you can get or fetch the JSON POST data using PHP. Set the content type to application/jsonĬurl_setopt ( $ch, CURLOPT_HTTPHEADER , array( 'Content-Type:application/json' )) Ĭurl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ) Ĭurl_close ( $ch ) Receive JSON POST Data using PHP Attach encoded JSON string to the POST fieldsĬurl_setopt ( $ch, CURLOPT_POSTFIELDS, $payload ) $payload = json_encode (array( "user" => $data )) Finally, the curl_exec() function is used to execute the POST request.Return the response as a string instead of outputting it using the CURLOPT_RETURNTRANSFER option.Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option.Setup data in PHP array and encode into a JSON string using json_encode().Initiate new cURL resource using curl_init().Specify the URL ( $url) where the JSON data to be sent.The following example makes an HTTP POST request and send the JSON data to URL with cURL in PHP. In this tutorial, we will show you how to POST JSON data using PHP cURL and get JSON data in PHP. PHP cURL makes it easy to POST JSON data to URL. When you working with web services and APIs, sending JSON data via POST request is the most required functionality. The JSON data format is mostly used in web services to interchange data through API. JSON is the most popular data format for exchanging data between a browser and a server.
0 Comments
Leave a Reply. |