无法遵循使用卷曲时的open_basedir设置的位置(URL) - API回调失败 [英] Unable to follow a location (URL) using cURL when open_basedir is set - API callback failing

查看:258
本文介绍了无法遵循使用卷曲时的open_basedir设置的位置(URL) - API回调失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现有张贴值API 结果的方法
根据API文档,表格已被发布到API URL(方法=POST和行动=API_URL)与一些输入字段和客户令牌场。该API进程,然后张贴回应我的服务器上的文件callback.php(固定 - 不能改变它)。该页面重定向到URL API,然后回来callback.php。我可以在该文件访问使用 $ _ POST 张贴的丘壑。这是所有关于现有的方法,它工作正常。

Existing method of posting values to API
As per the API documentation, a form has to be posted to the API URL (method="POST" and action="API_URL") with some input fields and a customer token field. The API processes and then posts response to a callback.php file on my server (fixed - can't change it). The page redirects to the API URL and then comes back to callback.php. I can access the posted vals using $_POST in that file. That's all about the existing method and it works fine.

服务器端后隐藏客户令牌结果
出于安全原因,我试图从服务器端的这个时候发布。

Server side post to hide the customer token
For security reasons, I am trying to post from server side this time.

问题结果
回拨没有发生(内部callback.php文件不执行code)。

The Problem
Callback is not happening (code inside callback.php file not executing).

与卷曲sturggling很难张贴到API和接收回调后,我意识到,open_basedir的是我的服务器,由于其 CURLOPT_FOLLOWLOCATION 设置。我发现下面的code这似乎是能够做到即使 safe_mode设置是发布的open_basedir 设置,只要

After sturggling hard with cURL to post to the API and receive callback, I realized that open_basedir is set in my server due to which CURLOPT_FOLLOWLOCATION. I found the following code which seems to be able to accomplish the posting even if safe_mode is On or open_basedir is set, provided that

我们一般知道我们会
  重定向到

we know generally where we'll be redirected to

请通过下面的code,告诉我什么是在这里的意思,如果我们知道一般我们会被重定向到。难道是网址,其中API将重定向到处理完成后?然后,是的,我知道,它有送回调到我的服务器上的文件callback.php,但没有发生。
  -

Please go through the code below and tell me what is meant here by if we know generally where we'll be redirected to. Is it the URL where the API will redirect to after the processing completes? Then yes I know, it has to send callback to a callback.php file on my server, but that is not happening. :-

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;
    }
}

在运行code,它进入conditon其中, $ HTTP code 既不是 301 也不 302 (这是 200 在我的情况)。和打印$ DATA给出了以下内容: -

On running the code, it enters into the conditon where $http_code is neither 301 nor 302 (it is 200 in my case). And printing the $data gives the following:-

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

帮助结果
帮我家伙..结果
任何code更改此需要的?结果
将我的情况袅袅不工作? (这是一个异步API - 。它触发时,它的完成回调函数的原始请求没有收到这种设置的返回值)

Help
Help me guys..
Any code changes to this needed?
Will cURL not work in my case? (it is an asynchronous API - it triggers the callback when it's done. The original request does not receive a return value in this kind of setup.)

谢谢
Sandeepan

Thanks Sandeepan

推荐答案

您应该检查什么的网址是卷曲请求(回声$ NEW_URL;)

You should check what the URL was cURL requested (echo $new_url;)

这篇关于无法遵循使用卷曲时的open_basedir设置的位置(URL) - API回调失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆