【问题标题】:Getting CURL post parameters right (PHP)正确获取 CURL 后参数 (PHP)
【发布时间】:2017-02-17 17:05:58
【问题描述】:

我正在尝试使用 CURL 向linkedin API 发送一个发布请求。出于某种原因,我收到回复说我缺少一个变量。

array:2 [▼
  "error" => "missing_parameter"
  "error_description" => "A required parameter "client_id" is missing"
]

这是我的代码,我可以向您保证已设置 cliend_id。

    $code = $request->get("code");
    $state = $request->get("state");
    $redirect_uri = "http://example.com/linkedin/callback";
    $client_id = "1242435657";
    $client_secret = "XXXXXXXXXX";

    $url = "https://www.linkedin.com/oauth/v2/accessToken";

    $params = array(
        "grant_type" => 'authorization_code',
        "code" => $code,
        "redirect_uri" => $redirect_uri,
        "client_id" => $client_id,
        "client_secret" => $client_key,
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    // create an array from the data that is sent back from the API
    $result = json_decode($output, 1);  

无论如何我可以调试这个帖子请求吗?

【问题讨论】:

  • 尝试将curl_setopt($ch, CURLOPT_POST, true);更改为curl_setopt($ch, CURLOPT_POST, count($params));
  • @BojanRadaković 这不会改变任何事情
  • 我在不记名令牌身份验证方面遇到了类似的问题。就我而言,我可以看到 POST 数据没有到达服务器。你找到解决问题的方法了吗?有没有办法用 curl 调试发布数据请求? stackoverflow.com/questions/49736998/…

标签: php curl post linkedin linkedin-api


【解决方案1】:

删除最后一个属性末尾的逗号,然后再试一次?

$params = array(
        "grant_type" => 'authorization_code',
        "code" => $code,
        "redirect_uri" => $redirect_uri,
        "client_id" => $client_id,
        "client_secret" => $client_key, <=========
    );

【讨论】:

  • 这不会改变任何东西。
  • 也许您应该将它们作为 GET 请求发送。我在 url 上试过这个:linkedin.com/oauth/v2/accessToken/… 这是我收到的错误:{"error":"invalid_client_id","error_description":"The pass in client_id is invalid \" 123\""} 。至少现在没有说:{"error":"missing_parameter","error_description":"A required parameter \"client_id\" is missing"}
猜你喜欢
  • 2016-10-16
  • 2017-09-12
  • 2020-06-05
  • 2013-06-27
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
相关资源
最近更新 更多