user285594
user285594

Reputation:

Why the PHP curl is not working? When it works from command line curl?

When i do it from command line as below it works:

$ curl -X "POST" "https://ABCD/login/oauth2/access_token"  -H "Authorization: Basic XXXX="  -H "Content-Type:application/x-www-form-urlencoded"  --data-urlencode "realm=XXX"  --data-urlencode "XXX=XXX"

But when i do it from PHP its not working:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ABCD/login/oauth2/access_token');
curl_setopt($ch, CURLOPT_POST, 1);

$ss = array(
  'realm' => 'XXX',
  'XXX'=>'XXX'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss));    

curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 


curl_setopt($ch, CURLOPT_HTTPHEADER, 
array(
  'Authorization: Basic XXXX=',
  'Content-Type: application/x-www-form-urlencoded',     
));

$result=curl_exec ($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);

Any idea what is that i am doing wrong? I get HTTP status that "Required parameters or body is missing or incorrect."

Upvotes: 0

Views: 760

Answers (1)

Quentin
Quentin

Reputation: 944052

Your command line says --data-urlencode — URL encoding

Your PHP says 'Content-Type: application/x-www-form-urlencoded', — so you say you are URL encoding the data

It also says curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ss)); — so you are JSON encoding it and not URL encoding it.

Send the URL encoded data you claim to be sending.

Upvotes: 1

Related Questions