如何在google +品牌页面上自动发布?

时间:2017-10-02 18:32:43

标签: php google-api google-plus google-oauth google-oauth-java-client

我使用的是以下代码,但代码无效。我想在google +品牌页面上更新我的网站帖子。如何更新品牌页面上的帖子并修复下面的代码?

给我解决方案或示例代码。我非常需要它。我必须做到。帮我解决它并给我提示,技巧和方法。感谢

代码:

<?php


// REQUIRED PARAMETERS

$status = 'http://www.mylink.com';

$email = 'your@email.com';
$pass = 'yourpassw0rd';

$pageid = false;
$cookies = 'cookie.txt';
$sleeptime = 0;
$uagent = 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)';
$pc_uagent = 'Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1';
$debug = FALSE;

function tidy($str) {
    return rtrim($str, "&");
}

/**
 * Handle cookie file
 */
@unlink($cookies); //delete previous cookie file if exists
touch($cookies); //create a cookie file

/**
 * MAIN BLOCK
 * login_data() just collects login form info
 * login($postdata) logs you in and you can do pretty much anything you want from here on
 */
login(login_data());
sleep($sleeptime);
if ($pageid) {
    update_page_status();
} else {
    update_profile_status();
} //update status with $GLOBAL['status'];
sleep($sleeptime);
logout(); //optional - log out

/**
 * 1. GET: http://plus.google.com/
 * Parse the webpage and collect form data
 * @return array (string postdata, string postaction)
 */
function login_data() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['uagent']);
    curl_setopt($ch, CURLOPT_URL, "https://plus.google.com/");
    curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $buf = utf8_decode(html_entity_decode(curl_exec($ch)));
    $buf = str_replace( '&', '&', $buf ); // just in case any correctly encoded
    $buf = str_replace( '&', '&', $buf ); // now encode them all again
    curl_close($ch);

    echo "\n[+] Sending GET request to: https://plus.google.com/\n\n";

    $toreturn = '';

    $doc = new DOMDocument;
    $doc->loadxml($buf);
    $inputs = $doc->getElementsByTagName('input');
    foreach ($inputs as $input) {
    switch ($input->getAttribute('name')) {
        case 'Email':
        $toreturn .= 'Email=' . urlencode($GLOBALS['email']) . '&';
        break;
        case 'Passwd':
        $toreturn .= 'Passwd=' . urlencode($GLOBALS['pass']) . '&';
        break;
        default:
        $toreturn .= $input->getAttribute('name') . '=' . urlencode($input->getAttribute('value')) . '&';
    }
    }
    return array(tidy($toreturn), $doc->getElementsByTagName('form')->item(0)->getAttribute('action'));
}

/**
 * 2. POST login: https://accounts.google.com/ServiceLoginAuth
 */
function login($postdata) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['uagent']);
    curl_setopt($ch, CURLOPT_URL, $postdata[1]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata[0]);
    $buf = curl_exec($ch); #this is not the g+ home page, because the b**** doesn't redirect properly
    curl_close($ch);
    if ($GLOBALS['debug']) {
    echo $buf;
    }

    echo "\n[+] Sending POST request to: " . $postdata[1] . "\n\n";
}

/**
 * 3. GET status update form:
 * Parse the webpage and collect form data
 */
function update_profile_status() {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['uagent']);
    curl_setopt($ch, CURLOPT_URL, 'https://m.google.com/app/plus/?v=compose&group=m1c&hideloc=1');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $buf = utf8_decode(html_entity_decode(str_replace('&', '', curl_exec($ch))));
    $header = curl_getinfo($ch);
    curl_close($ch);
    if ($GLOBALS['debug']) {
    echo $buf;
    }

    $params = '';
    $doc = new DOMDocument;
    $doc->loadxml($buf);
    $inputs = $doc->getElementsByTagName('input');
    foreach ($inputs as $input) {
    if (($input->getAttribute('name') != 'editcircles')) {
        $params .= $input->getAttribute('name') . '=' . urlencode($input->getAttribute('value')) . '&';
    }
    }
    $params .= 'newcontent=' . urlencode($GLOBALS['status']);
    //$baseurl = $doc->getElementsByTagName('base')->item(0)->getAttribute('href');
    $baseurl = 'https://m.google.com' . parse_url($header['url'], PHP_URL_PATH);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['uagent']);
    //delete group=b0& in the line below, to post just to your circles, not to public
    curl_setopt($ch, CURLOPT_URL, $baseurl . '?v=compose&group=m1c&group=b0&hideloc=1&a=post');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, $baseurl . '?v=compose&group=m1c&group=b0&hideloc=1');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $buf = curl_exec($ch);
    $header = curl_getinfo($ch);
    curl_close($ch);
    if ($GLOBALS['debug']) {
    echo $buf;
    }

    echo "\n[+] POST Updating status on: " . $baseurl . "\n\n";
}

/**
 * Not implemented yet!
 * just ignore this function for now
 */
function update_page_status() {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['pc_uagent']);
    curl_setopt($ch, CURLOPT_URL, 'https://plus.google.com/u/0/b/' . $GLOBALS['pageid'] . '/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $buf = utf8_decode(html_entity_decode(str_replace('&', '', curl_exec($ch))));
    curl_close($ch);
    if ($GLOBALS['debug']) {
    echo $buf;
    }
}

/**
 * 3. GET logout:
 * Just logout to look more human like and reset cookie :)
 */
function logout() {
    echo "\n[+] GET Logging out: \n\n";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
    curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['uagent']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/m/logout');
    $buf = curl_exec($ch);
    curl_close($ch);
    if ($GLOBALS['debug']) {
    echo $buf;
    }
}

?>

它也显示我的错误。
错误是:

[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): StartTag: invalid element name in Entity, line: 1 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): xmlParseEntityRef: no name in Entity, line: 1 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): xmlParseEntityRef: no name in Entity, line: 1 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Opening and ending tag mismatch: link line 1 and head in Entity, line: 1 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Premature end of data in tag input line 1 in Entity, line: 2 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Premature end of data in tag div line 1 in Entity, line: 2 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Premature end of data in tag body line 1 in Entity, line: 2 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Premature end of data in tag base line 1 in Entity, line: 2 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Premature end of data in tag head line 1 in Entity, line: 2 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Warning:  DOMDocument::loadXML(): Premature end of data in tag html line 1 in Entity, line: 2 in /home/masudtoo/public_html/ytapipro/index.php on line 67
[02-Oct-2017 18:33:52 UTC] PHP Fatal error:  Call to a member function getAttribute() on null in /home/masudtoo/public_html/ytapipro/index.php on line 81

1 个答案:

答案 0 :(得分:0)

切勿使用Google凭据执行此操作, 而不是创建api密钥 要么 您可以使用oauth令牌发布。

以下是获取访问令牌的方法 https://developers.google.com/+/web/api/rest/oauth#apikey api写一篇文章 https://developers.google.com/+/domains/posts/creating