1

Hello great stackoverflow, am trying to re-write the api below from javascript to php in other to be able to make json curl but displays error below

Warning: curl_setopt() expects exactly 3 parameters, 4 given in C:\xampp\htdocs\firstcare\curl.php on line 15

js curl

curl -X POST
 "http://my_api.com/accesstoken?grant_type=client_credentials" 
  -H "Content-Type: application/x-www-form-urlencoded" 
  -d 'client_id=$myClient_id' 
  -d 'client_secret=$myClient_secret

php curl conversion

<?php
// 0 means unlimited timeout
ini_set('max_execution_time', 0);

$data = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
        'client_id' => 'myclient_id',
        'client_secret'  => 'myclient_secret'           
);
$url='http://my_api.com/accesstoken?grant_type=client_credentials';
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url,$data);
$result=curl_exec($ch);
curl_close($ch);

$json = json_decode($result, true);


echo '<pre>' . print_r($json, true) . '</pre>';

?>

Thanks

3 Answers 3

1

Error was thrown because curl_setopt requires 3 parameters - you've passed 4 here:

curl_setopt($ch, CURLOPT_URL,$url,$data);
Sign up to request clarification or add additional context in comments.

1 Comment

how do i make it 3 parameters and ensures that the code works
0

May I suggeest guzzlehttp, wrapper for curll...also u seem to be passing headers in $data as the forth paramenter remove $data. If u want to sent cllient id as GET append the $url with http_build_query()

1 Comment

So in what way is it not working, what error. Does the credentialls require post or get to be sent
0

Try these codes

<?php
// 0 means unlimited timeout
ini_set('max_execution_time', 0);

$postdata = array(
        'client_id' => 'myclient_id',
        'client_secret'  => 'myclient_secret'           
);

$header = array(
       'Content-Type' => 'application/x-www-form-urlencoded',
);

$url='http://my_api.com/accesstoken?grant_type=client_credentials';
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

$result=curl_exec($ch);
curl_close($ch);

$json = json_decode($result, true);


echo '<pre>' . print_r($json, true) . '</pre>';

?>

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.