【问题标题】:php curl get redirected url after get request [duplicate]php curl在获取请求后获取重定向的url [重复]
【发布时间】:2018-09-11 22:12:43
【问题描述】:

我正在使用 bitbucket api,当我输入以下 url 时,我得到了一个访问令牌:'https://bitbucket.org/site/oauth2/authorize?client_id={client_id}&response_type=code ' 并将我重定向到 https://api.bitbucket.org/2.0/repositories/{my-team}/?code={code} 我需要从重定向 url 中获取该 {code} 我通过以下方式使用 curl:

public function getCode()
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://bitbucket.org/site/oauth2/authorize?client_id=".config('app.bitbucket_key')."&response_type=code");

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        return 'Error: invalid credentials';
    }

    return $result;
}

但结果我是真的。我想在重定向 url 中获取代码查询参数。我该怎么做呢

编辑:

当我与curl_getinfo() 核对时,我发现它会将我重定向到另一个网址,即:https://bitbucket.org/account/signin/?next=/site/oauth2/authorize%3Fclient_id%3D{my_client_id} %26response_type%3Dcode

【问题讨论】:

    标签: php curl bitbucket bitbucket-api


    【解决方案1】:

    我对相关的 api 一无所知,但要从 curl 获取 html 响应,您需要设置返回传输选项。

        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true); 
    

    【讨论】:

    • 我得到一个空字符串,但是当我在该网址上手动输入时,它会将我重定向到我需要的页面
    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 2011-03-05
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2018-01-09
    相关资源
    最近更新 更多