简体   繁体   中英

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."

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM