【问题标题】:Error in Autodesk API call through PHP with CURL使用 CURL 通过 PHP 调用 Autodesk API 时出错
【发布时间】:2016-01-29 16:11:32
【问题描述】:

我试图在 CURL 的帮助下通过 PHP 调用 Autodesk Authentication API,但我不断收到错误的响应。 不知道出了什么问题,谁能建议如何在 PHP 中借助 CURL 调用 REST 服务?

我的代码是这样的——

$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);

输出是 -

{"result":false}

我用 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.

【问题讨论】:

  • x-www- form-..? mime 类型中没有空格...
  • 我写的代码中没有空格。这是一个错字。我在这里添加了响应信息。
  • 最后找到问题了吗?

标签: php curl restful-authentication autodesk


【解决方案1】:

尝试添加:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

这告诉 curl 将传输作为 curl_exec() 的返回值的字符串返回,而不是直接输出。您可以阅读有关 curl 选项的更多信息 here

【讨论】:

  • 非常感谢您的快速回复。我试过了,但仍然得到相同的响应
【解决方案2】:

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

$data_string = json_encode($data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2014-11-10
    • 2021-10-16
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多