0
curl -H 'content-type: application/json' --insecure -d '{"client_id":"w44p0d00.apps.2do2go", "client_secret":"mvlldlsfKLLSczxc12Kcks910cccs", "grant_type":"client_credentials", "scope": "anonymous"}' https://someurl.com/oauth/token

This command line cURL works perfectly. How I can do the same in PHP?

curl_setopt($ch, CURLOPT_URL, 'https://someurl.com/oauth/token'); //this my url
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json')); //its -H
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

2 Answers 2

2

This is equivalent to your --insecure param:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Its equivalent to your -d param at where you are posting json object.

$json = '{"client_id":"w44p0d00.apps.2do2go", "client_secret":"mvlldlsfKLLSczxc12Kcks910cccs", "grant_type":"client_credentials", "scope": "anonymous"}';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

After adding these to your existing curl, do the below to perform the curl and print the data:

$response = curl_exec($ch);
curl_close($ch);
print $response;
Sign up to request clarification or add additional context in comments.

Comments

0
$url = 'https://someurl.com/oauth/token';
$fields = array(
    'client_id' => urlencode("w44p0d00.apps.2do2go"),
     ....
);


foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json'));
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.