成功后cURL PHP代码重定向

时间:2015-06-08 20:40:44

标签: php curl

我使用cURL创建了一个Facebook登录类型代码,我想知道如何在成功登录Facebook后重定向回我的网站。代码如下所示:

<?PHP
    $login_email = '****';
    $login_pass = '****';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&  pass='.urlencode($login_pass).'&login=Login');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    $page = curl_exec($ch) or die(curl_error($ch));

    echo $page;
?>

1 个答案:

答案 0 :(得分:0)

首先,您不必使用cURL登录Facebook。 Facebook提供its own API.

您可以检查该API中的$session变量是否成功,然后调用header方法。另请参阅:FaceBookRedirectLoginHelper

如果您要重定向,请使用header() function

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>