Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

how to convert GET method to POST method in cURL in PHP?

Dear Experts,
I use PHP, I need to get a token from a link using below information.
I did that with GET method with below code but I need to do that with POST method
How can I do that? Thank you

Method : POST
Authorization  Type : oauth2
Grant Type : password
Content-Type : application/x-www-form-urlencoded
RequestUrl :xxxxxxxxxxx
URL: https://www.api-url-here.com/auth-function-here


Service Input Parameters:

username as string
password as string
client_Id as string

Service Return Parameters:

access_token as string
token_type as string
expires_in as string

thank you in advance.



//Initialize cURL.
$ch = curl_init();
 
//Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, 'https://www.api-url-here.com/auth-function-here?grant_type=password&username=MickeyMouse&password=letmein&client_id=00001');

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
 
//Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
//Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 
//Execute the request.
$return = curl_exec($ch);
 
//Close the cURL handle.
curl_close($ch);
 
//Print the data out onto the page.
$data = json_decode($return);

var_dump ($data);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Thank you
You are welcome.