dongman5539 2010-08-27 19:41
浏览 42
已采纳

使用Curl登录NYTimes.com的问题

UPDATE: Turns out my code works. Browser was caching previous failed response. Thanks for the pointers.

I'm building a prototype and one thing I'd like to do is perform a service if the user is a valid member of NYTimes.com by providing their credentials.

Using curl, I'm trying to perform a login to the site, and then check for success or failure.

My code, below, doesn't return errors but drops me back at the login page:

<?php
class Login {

    function Verify() {
        print $this->getContent();
    }

    function getContent() {

        $url    = 'http://www.nytimes.com/auth/login';

        // URI can be any NYT web page to be redirected to upon successful login
        // SAVEOPTION and Submit2 are Optional but in original web form so included here

        $fields = array(
            'is_continue'=> 'true',
            'USERID'     => urlencode('ENTER_YOUR_USERNAME'),
            'PASSWORD'   => urlencode('ENTER_A_PASSWORD'),
            'URI'        => urlencode('http://www.nytimes.com/robots.txt'), 
            'OQ'         => '',
            'OP'         => '',
            'SAVEOPTION' => 'YES',
            'Submit2'    => 'Log In'
        );

        $fields_string = '';

        if(!$curld = curl_init($url)) {
            echo "Could not connect to the specified resource";
               exit;
        }
        $ch        = curl_init();
        $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
        rtrim($fields_string,'&');

        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "curl_login_cookie.txt");

        curl_setopt($ch ,CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

        ob_start();
        curl_exec ($ch);

        curl_close ($ch);
        $result = ob_get_contents();
        ob_end_clean();

        return $result;
    }
}

$login  = new Login;
$result = $login->Verify();
?>

Any pointers, or suggestions welcome.

  • 写回答

1条回答 默认 最新

  • dongzi0850 2010-08-27 19:52
    关注

    You need to check that the result of curl_exec is not false (which it'll be if the execution failed).

    If it's succeeding, try checking the results of curl_errno and curl_getinfo to see information about the operation.

    Also, it might be better to set the CURLOPT_RETURNTRANSFER option on your curl object instead of using output buffering to capture the result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度