0

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.

Aron Cederholm
  • 5,237
  • 4
  • 34
  • 53
akalogiros
  • 75
  • 6
  • 7
    Possible duplicate of [PHP + curl, HTTP POST sample code?](https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – Daan Oct 16 '17 at 13:38
  • 2
    A basic google search gives you the answer ... – marco-a Oct 16 '17 at 13:39

1 Answers1

1

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);
Aron Cederholm
  • 5,237
  • 4
  • 34
  • 53