![]() As per our requirement, we need to set the request as GET or POST.?> Some point regarding the implementation of basic HTTP authentication using PHP cURL $payload = json_encode(array("filter" => $payload_data)) Here we will create a simple array with some payload data and encode it in json format and pass in CURLOPT_POSTFIELDS. ![]() ![]() So let's see how to post payload data in cURL request. This data is also known as payload, in terms of API. In some case, we need to post some data along with the cURL request. The code is given below: How to pass data or payload in basic HTTP authentication using PHP cURL Here we will not use 'Basic' word and encoded authentication. There is another native way to directly call the cURL by passing simple authentication. As we know that cURL is primarily use to call API and here we will use PHP cURL to make the request. Now let's see the simple code to make an API request using PHP curl with HTTP basic authentication. Password is similar to access secret Send API request with HTTP basic authentication using PHP cURL So don't get confused with the below term because both are the same. Some API providers use the word access token and access secret that is similar to username and password. 401 unauthorized.Ĥ01 Unauthorized: You need a valid user and password. Now if we send the PHP cURL request which is protected by HTTP authentication, then we will get an error of unauthorize access i.e. So the basic authentication comes into the picture.īasic authentication requires a basic authorization header with the HTTP request which send in encoded format.įor example, the authorization header contains username and password in base64-encoded format followed by Basic word. But these days, every request requires some kind of authorization to get a response. The server can handle the request only if there is no any protection of password or the server allows unauthorize access. In a simple HTTP request, the client sends the request to the server, and the server is responsible to serve the request. The basic authentication works with HTTP protocol to authenticate the client's HTTP request. I hope you have understood the code.These days, basic authentication is very common while making any API call to authenticate the request. So thats all about How To Post Data with Authorization Headers In PHP Using cURL. And then we can see CURLOPT_HTTPHEADER option set in the curl, This is where we are telling cURL to include Authorization Headers in the Request. Here you may have noted the ‘ Content-Type: application/x-And last but important point is Authorization attribute in the headers, this is where we send the Authorization key to authenticate the request with the API server. Lets go through some important points in the above code. $authHeaders = 'Authorization: '.$authKey Ĭurl_setopt($curl, CURLOPT_HTTPHEADER,$authHeaders) $authHeaders = 'Content-Type: application/x-www-form-urlencoded' authKey can be any value as provided in API Panel = $key.'='.$value.'&' Ĭurl_setopt($curl,CURLOPT_POST, count($post_fields)) Ĭurl_setopt($curl,CURLOPT_RETURNTRANSFER ,true) Ĭurl_setopt($curl,CURLOPT_POSTFIELDS, $post_fields_string ) url-ify the data for the x-www-form-urlencoded POST data We can use CURLOPT_HTTPHEADER option in the cURL to add Authorization Headers in the POST Request.įollowing is the code used to send PHP cURL POST request with Authorization Headers: curl_close:- After request gets executed, then we need to close cURL request session and free the all resources used by cURL.ĬURL POST Data With Authorization Headers.curl_exec:- In this step, actual request is executed with the options set in the second step.curl_setopt:- This step is used to set cURL request options. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |