php - 如何使用 PHP cURL 创建新的谷歌警报,将其传送到 Feed

标签 php curl google-api-client google-alerts

我只需要 - 它是创建连接到我的帐户的谷歌警报,它应该传送到我的 Feed。 对于身份验证,我正在使用 curl

function googleAuthenticate($username, $password, $source, $service = 'alerts') {
$session_token = $source . '_' . $service . '_auth_token';

if (isset($_SESSION[$session_token])) {
    echo 'уже есть';
    return $_SESSION[$session_token];
}

// get an authorization token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE')
    . "&Email=" . urlencode($username)
    . "&Passwd=" . urlencode($password)
    . "&source=" . urlencode($source)
    . "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$response = curl_exec($ch);
curl_close($ch);

if (strpos($response, '200 OK') === false) {
    return false;
}

// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);
if (!$matches[2]) {
    return false;
}

$_SESSION[$session_token] = $matches[2];
return $matches[2];}

但我无法在 google.com/alerts 上填写表格

最佳答案

基于此页面的代码:

Login to Google with PHP and Curl, Cookie turned off?

function createAlert($search) {

        $USERNAME = 'EMAIL_ADDRESS';
        $PASSWORD = 'PASSWORD';
        $COOKIEFILE = 'cookies.txt';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);

        curl_setopt($ch, CURLOPT_URL,
        'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
        $data = curl_exec($ch);

        $formFields = $this->getFormFields($data);

        $formFields['Email']  = $USERNAME;
        $formFields['Passwd'] = $PASSWORD;
        unset($formFields['PersistentCookie']);

        $post_string = '';
        foreach($formFields as $key => $value) {
        $post_string .= $key . '=' . urlencode($value) . '&';
        }

        $post_string = substr($post_string, 0, -1);

        curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

        $result = curl_exec($ch);

        if (strpos($result, '<title>Redirecting') === false) {
            var_dump($result);
            die("Login failed");
        } else {
            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts');
            curl_setopt($ch, CURLOPT_POST, 0);
            curl_setopt($ch, CURLOPT_POSTFIELDS, null);

            $result = curl_exec($ch);

            // Create alert

            preg_match('/<input type="hidden" name="x" value="([^"]+)"/', $result, $matches);

            $post = array(
                "x" => $matches[1],     // anti-XSRF key
                "q" => $search,     // Search term  
                "t" => 7,       // Result type (everything)
                "f" => 1,       // Frequency (once a day)
                "l" => 1,       // How many (all results)
                "e" => "feed"       // Type of delivery (RSS)
            );

            $post_string = '';

            foreach($post as $key => $value) {
                $post_string .= $key . '=' . urlencode($value) . '&';
            }

            $post_string = substr($post_string, 0, -1);

            curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/create');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

            $result = curl_exec($ch);
            $matches = array();
            preg_match('#<a href="(http://www.google.com/alerts/feeds/[\d/]+)"#', $result, $matches);

            $top_alert = $matches[1];

            return $top_alert;
        }
    }


    function getFormFields($data)
    {
        if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
            $inputs = $this->getInputs($matches[1]);

            return $inputs;
        } else {
            die("didn't find login form");
        }
    }

    function getInputs($form)
    {
        $inputs = array();

        $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

        if ($elements > 0) {
            for($i = 0; $i < $elements; $i++) {
                $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

                if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                    $name  = $name[1];
                    $value = '';

                    if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                        $value = $value[1];
                    }

                    $inputs[$name] = $value;
                }
            }
        }

        return $inputs;
    }

关于php - 如何使用 PHP cURL 创建新的谷歌警报,将其传送到 Feed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528747/

相关文章:

php - 如何将 "blog"slug 添加到 wordpress 帖子?

django - 使用 json 使用 djangorestframework 将图像上传到 imagefield 并使用 CURL 进行测试

php - 安装 Composer 时出错 - curl : (23) Failed writing body (0 ! = 16133)

android - 使用 Firebase 的 GoogleApiClient 中出现罕见的 NullPointerException

javascript - jQuery UI Portlet 在数据库上保存状态

php - SMTP 在 WAMP 服务器上不起作用

google-api - Google 登录网站 : read user's phone number

android - GoogleApiClient 中的 getLastLocation() 始终为 null

php - 我什么时候应该在 Joomla 中使用 setUserState?

c++ - 使用 cURLpp 进行多次下载的进度指示器