你好伟大的stackoverflow,我试图重写下面的api从javascript到php在其他能够使json卷曲,但在下面显示错误
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>';
?>
由于
答案 0 :(得分:1)
抛出错误是因为curl_setopt
需要3个参数 - 你在这里传递了4个:
curl_setopt($ch, CURLOPT_URL,$url,$data);
答案 1 :(得分:0)
我可以建议guzzlehttp,curll的包装器......你似乎也在$ data中传递标题,因为第四个参数删除了$ data。如果你想发送cllient id作为GET附加$ url与http_build_query()
答案 2 :(得分:0)
试试这些代码
<?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>';
?>