I was wondering how to simulate a curl
command in PHP
. I want to simulate this:
curl -X POST https://example.com/token\?\
client_id\=your_client_id\&\
client_secret\=your_client_secret\&\
grant_type\=client_credentials\&\
scope\=public
I tried this:
curl_setopt($s, CURLOPT_POST,array(
'client_id=my_id',
'client_secret/=my_secred',
'grant_type/=client_credentials',
'scope/=public'
));
But I didn't have any luck.
Use http_build_query
to encode your data to a query string, and then set the CURLOPT_POSTFIELDS
option, along with CURLOPT_POST
and CURLOPT_URL
params, and finally send it.
$s = curl_init();
curl_setopt($s, CURLOPT_URL, 'https://example.com/token');
curl_setopt($s, CURLOPT_POST, 1);
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query([
'client_id' => 'my_id',
'client_secret' => 'my_secred',
'grant_type' => 'client_credentials',
'scope' => 'public'
]));
curl_exec($s);
curl_close($s);