如何通过cUrl

时间:2016-04-06 23:20:56

标签: php wordpress curl cookies

我正在尝试在单独的网站上加载Wordpress页面。

我在网上找到了这个代码,它使用cUrl来获取wordpress网站,登录,保存cookie并加载cookie。因此,当我在单独的站点上加载脚本时,应该就像我登录到Wordpress站点一样。

 <?php 
 function curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file ){

    if( !function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' )){
        $m = "cUrl is not vailable in you PHP server.";
        echo $m;
    }

    // Preparing postdata for wordpress login
    $data = "log=". $login_user ."&pwd=" . $login_pass . "&wp-submit=Log%20In&redirect_to=" . $visit_url;

    // Intialize cURL
    $ch = curl_init();

    // Url to use
    curl_setopt( $ch, CURLOPT_URL, $login_url );

    // Set the cookies for the login in a cookie file.
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie_file );

    // Set SSL to false
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

    // User agent
    curl_setopt( $ch, CURLOPT_USERAGENT, $http_agent );

    // Maximum time cURL will wait for get response. in seconds
    curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );

    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );

    // Return or echo the execution
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

    // Set Http referer.
    curl_setopt( $ch, CURLOPT_REFERER, $login_url );

    // Post fields to the login url
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
    curl_setopt( $ch, CURLOPT_POST, 1);

    // Save the return in a variable
    $content = curl_exec ($ch);

    /*
    ** if you need to visit another url ,you can do it here.
    ** curl_setopt( $ch, CURLOPT_URL, 'a new url address or a file download url' );
    ** $content = curl_exec ($ch);
    */

    // Close the cURL.
    curl_close( $ch );

    // You can echo or return the page data here. 
    echo $content;
}

// Username for login
$login_user = "username";


/*
** Password for this username
*/
$login_pass = "password";


/*
** Login url address.
*/
$login_url = "http://wp.co.uk/wp-login.php";


/*
** Which page you want to visit after login.
** WordPress redirect their user automatically after login to this page
** if you do not assign a visit page,
** then the result for this login will return '1'.
** That means you have logged in successfully.
** Visit url is ipmportant to get the content.
*/
$visit_url = urlencode( 'http://wp.co.uk/loggedinpage/' );


/*
** Cookie vaiable
*/
$cookie_file = "cookies/cookie.txt";


/*
** Set HTTP user agent.
*/
$http_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";

// Test the call
curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file );

        ?>

Wordpress网站被加载到单独的网站上,cookie被保存到cookies / cookie.txt,但我没有登录。即使我尝试手动登录,我也无法登录。

如何通过cUrl登录Wordpress并在其他网站上加载登录网站。

0 个答案:

没有答案