【问题标题】:Unable to follow a location (URL) using cURL when open_basedir is set - API callback failing设置 open_basedir 时无法使用 cURL 跟踪位置 (URL) - API 回调失败
【发布时间】:2011-04-06 17:45:54
【问题描述】:

将值发布到 API 的现有方法
根据 API 文档,必须将带有一些输入字段和客户令牌字段的表单发布到 API URL(method="POST" 和 action="API_URL")。 API 处理然后将响应发布到我服务器上的 callback.php 文件(已修复 - 无法更改)。页面重定向到 API URL,然后返回到 callback.php。我可以在该文件中使用 $_POST 访问发布的 vals。这就是现有方法的全部内容,并且效果很好。

隐藏客户令牌的服务器端帖子
出于安全原因,我这次尝试从服务器端发布。

问题
回调未发生(callback.php 文件中的代码未执行)。

在努力使用 cURL 发布到 API 并接收回调之后,我意识到我的服务器中设置了 open_basedir,因为 CURLOPT_FOLLOWLOCATION。我发现以下代码似乎能够完成发布,即使 safe_mode 设置为 Onopen_basedir,只要

我们通常知道我们会在哪里 重定向到

请通过下面的代码告诉我if we know generally where we'll be redirected to 的含义。它是处理完成后 API 将重定向到的 URL 吗?然后是的,我知道,它必须向我服务器上的 callback.php 文件发送回调,但这并没有发生。 :-

function curl($url, $postVars)
{
    $go = curl_init($url);
    curl_setopt ($go, CURLOPT_URL, $url);
    curl_setopt($go, CURLOPT_VERBOSE, 1);

    //follow on location problems
    if (ini_get('open_basedir') == '' && (ini_get('safe_mode')== 'Off'))
    {
        curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);
        $syn = curl_exec($go);
        if(curl_error($go))
            return false;
    }
    else
        $syn = curl_redir_exec($go, $postVars);
    curl_close($go);
    return $syn;
}

function curl_redir_exec($ch, $postVars)
{
    static $curl_loops = 0;
    static $curl_max_loops = 20;
    if ($curl_loops++>= $curl_max_loops)
    {
        $curl_loops = 0;
        return FALSE;
    }
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postVars);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $data = curl_exec($ch);
    if(curl_error($ch))
        return false;
    list($header, $data) = explode("\n\r", $data, 2);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    $redirect_page = "[0-9]*.html";
    $base_redirect = "http://example.com/";

    if ($http_code == 301 || $http_code == 302)
    {
        $matches = array();
        $pregs = eregi($redirect_page, $data, $matches);
        $new_url = $base_redirect . $matches[0];
        if (!$new_url)
        {
            //couldn't process the url to redirect to
            $curl_loops = 0;
            return $data;
        }
        curl_setopt($ch, CURLOPT_URL, $new_url);

        return curl_redir_exec($ch, $postVars);
    }
    else
    {
        $curl_loops=0;
        return $data;
    }
}

在运行代码时,它进入$http_code 既不是301 也不是302 的条件(在我的例子中是200)。并打印 $data 给出以下内容:-

HTTP/1.1 200 OK Date: Wed, 01 Sep 2010 10:02:44 GMT Server: Apache/2 X-Powered-By: PHP/5.2.11 Content-Length: 0 Connection: close Content-Type: text/html

帮助
帮帮我..
需要对此进行任何代码更改吗?
cURL 在我的情况下不起作用吗? (它是一个异步 API - 它在完成时触发回调。在这种设置中,原始请求不会收到返回值。)

谢谢 桑迪潘

【问题讨论】:

标签: php api asynchronous curl httpresponse


【解决方案1】:

您应该检查 cURL 请求的 URL 是什么 (echo $new_url;)

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多