简体   繁体   English

通过带有CURL的PHP​​通过Autodesk API调用时出错

[英]Error in Autodesk API call through PHP with CURL

I am trying to make call to Autodesk Authentication API through PHP with the help of CURL but I am continously getting false response. 我试图在CURL的帮助下通过PHP调用Autodesk Authentication API,但是我一直在得到错误的响应。 Not sure what is wrong, can anyone please suggest how to call REST services with the help of CURL in PHP? 不知道出了什么问题,有人可以建议如何借助PHP中的CURL调用REST服务吗?

My code is like this - 我的代码是这样的-

$url = 'https://developer.api.autodesk.com/authentication/v1/authenticate';
    $data = array("client_id" => $consumer_key,"client_secret" => $secret_key,"grant_type"=>$grant_type);

    $ch=curl_init($url);
    echo $ch;
    $data_string = json_encode($data);
    echo $data_string;
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type'=>'application/x-www-  form-urlencoded'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
    $result = curl_exec($ch);
    curl_close($ch);
    $data_return=array("result"=> $result);
    echo json_encode($data_return);

Output is - 输出是-

{"result":false}

I checked the information with curl_getinfo and it is like- array ( 'url' => 'https://developer.api.autodesk.com/authentication/v1/authenticate', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 0.34300000000000003, 'namelookup_time' => 0, 'connect_time' => 0.23400000000000001, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'redirect_url' => '', 'primary_ip' => '52.26.41.203', 'certinfo' => array ( ), 'primary_port' => 443, 'local_ip' => '192.168.1.106', 'local_port' => 62792, ) 我用curl_getinfo检查了信息,它就像array ( 'url' => 'https://developer.api.autodesk.com/authentication/v1/authenticate', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 0.34300000000000003, 'namelookup_time' => 0, 'connect_time' => 0.23400000000000001, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'redirect_url' => '', 'primary_ip' => '52.26.41.203', 'certinfo' => array ( ), 'primary_port' => 443, 'local_ip' => '192.168.1.106', 'local_port' => 62792, )

In reponse its showing "protocol.http.BadFormData" error.

Try adding: 尝试添加:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

This tells curl to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 这告诉curl将传输作为curl_exec()返回值的字符串返回,而不是直接将其输出。 You can read more about curl options here . 您可以在此处阅读有关curl选项的更多信息。

我不认为您需要对数据进行JSON编码...请尝试删除以下行:

$data_string = json_encode($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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