2

站点 A 有我所有的 php 类和函数:

这是我的操作 (profile.php) 页面 SITEA 中的内容:

$shopper = new Shopper($email);
$shopper->login($password);

$cInit = curl_init("http://siteA/test/login");
curl_setopt_array($cInit, array(
    CURLOPT_RETURNTRANSFER=>true,
    CURLOPT_POST=>true,
    CURLOPT_POSTFIELDS=>urlencode(implode("&", array("email" => $email, "password" => $password)))
    ));

    $content = curl_exec($cInit);
    $err     = curl_errno($cInit);
    $errmsg  = curl_error($cInit);

    $results = json_decode($content)
?>

站点 B 托管在不同的服务器上,它具有 form 和 curl 选项:

$email = $_POST['email'];
$password = $_POST['password'];

$shopper = new Shopper();
$token = $shopper->login($email, $password);

echo json_encode(array("token" => $token)); 

<form id='register' action='http://siteA/test/profile' onsubmit="return validateForm()" method='post' accept-charset='UTF-8'>
    <fieldset>
    <legend>Log In</legend><br/>
    <label for='email' >Email Address*:</label>
    <input type='text' name='email' id='email' maxlength="50" /><br/><br/>
    <label for="password">Password*:</label>  
    <input type="password" name="password" placeholder="password" required><br/><br/>
    <input type='submit' name='Submit' value='Submit' />
    </fieldset>
    </form>

令牌一直返回错误,到目前为止我的代码看起来是否正确?

4

1 回答 1

0

不是答案 | 可能有助于评估问题


我还不确定,只是看看我所看到的并与您交谈,但这也许可以帮助您评估。以下代码片段将使您在某种程度上更容易评估正在发生的事情。使用就像 a 一样简单var_dump,但结果在浏览器中更容易阅读,因为它只需要您的转储并在​​其周围放置一些漂亮的 HTML。它还接受与您输入的参数一样多的参数,因此可以轻松地一次评估多个变量。

function preDump() {    //  use string "noEcho" to just get a string return only
    $args = func_get_args();
    $doEcho = TRUE; $sb;
    if ($args) {
        $sb = '<div style="margin: 1em 0;"><fieldset style="display:inline-block;padding:0em 3em 1em 1em;"><legend><b>preDump: '.count($args).' Parameters Found.</b></legend>';
        foreach (func_get_args() as $arg) {
            if (gettype($arg) == 'string') if ($arg == 'noEcho') { $doEcho = FALSE; $sb = preg_replace('/(preDump: )[0-9]+/', 'preDump: '.(count($args)-1), $sb); continue; }
            $sb .= '<pre data-type="'.gettype($arg).'"';
            switch (gettype($arg)) {
                case "boolean":
                case "integer":
                    $sb .= ' data-dump="json_encode"><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')</b></p><p>';
                    $sb .= json_encode($arg);
                    break;
                case "string":
                    $sb .= ' data-dump="echo"><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')</b></p><p>';
                    $sb .= $arg;
                    break;
                default:
                    $sb .= ' data-dump="var_dump"';
                    if (is_object($arg)) $sb .= 'data-class="'.get_class($arg).'"';
                    $sb .= '><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')';
                    if (is_object($arg)) $sb .= ' ['.get_class($arg).']';
                    $sb .= '</b></p><p>';
                    ob_start();
                    var_dump($arg);
                    $sb .= ob_get_clean();
                    if (ob_get_length()) ob_end_clean();
            }
            $sb .= '</p></pre>';
        }
        $sb .= '</fieldset></div>';
    }
    else {
        $sb = '<div style="margin: 1em 0;"><fieldset style="display:inline-block;"><legend><b>preDump: [ERROR]</b></legend><h3>No Parameters Found</h3></fieldset></div>';
    }
    if ($doEcho) echo($sb);
    return $sb;
}

我建议这样做,因为我觉得这里缺少更多代码,并且您在 2 个不同的服务器上操作 2 个差异。因此,您可以按如下方式使用它:

站点 A

function preDump() { ... }

$shopper = new Shopper($email);
$shopper->login($password);

//  Maybe go ahead and test your results of shopper here
preDump($shopper); return;

$cInit = curl_init("http://siteA/test/login");
curl_setopt_array(
    $cInit,
    array(
        CURLOPT_RETURNTRANSFER=>true,
        CURLOPT_POST=>true,
        CURLOPT_POSTFIELDS=>urlencode(implode("&", array("email" => $email, "password" => $password)))
    )
);

$content = curl_exec($cInit);
$err     = curl_errno($cInit);
$errmsg  = curl_error($cInit);

$results = json_decode($content)
//  or, remove that line i put up above and test your results here
preDump($results); return;

//  you can even get the preDump results as a string and then echo as you see fit
$bob = preDump($results, 'noEcho'); //  string of 'noEcho' will cause the results to return as a string not be echoed into browser 
于 2013-11-15T14:45:05.450 回答