使用curl和json返回一个令牌

时间:2013-11-14 19:30:06

标签: php json curl

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

这是我的行动(profile.php)页面中的内容:

$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托管在具有表单和卷曲选项的其他服务器上:

$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>

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

1 个答案:

答案 0 :(得分:0)

不是回答|可能对评估问题有帮助


我还不确定,只是看到我所见过并与你交谈,但也许这可以帮助你评估。以下代码段将使您更容易评估某种程度上发生的情况。使用就像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差异。因此,您可以按如下方式使用它:

网站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