Franky So
Franky So

Reputation: 171

How to automatically post to google+ with php

i want to post in my Google plus when i create a post or something in my site

i have searching in Google API but i cant find something good stuff.

share if you have anything useful. thank you.

Upvotes: 3

Views: 2679

Answers (1)

iBrazilian2
iBrazilian2

Reputation: 2293

This way you'll have to share it yourself, it will not be automatically as I haven't gotten around to actually doing so but here is how you can post it by opening a javascript button.

function getPlus($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . rawurldecode($url) . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    $curl_results = curl_exec ($curl);
    curl_close ($curl);
    $json = json_decode($curl_results, true);
    return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}

Here is the button to open a window.

<a
href="https://plus.google.com/share?url=<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" 
onClick="return google_click(400, 300)" target="_blank" 
title="Share This on Google+"
>Share on Google+</a>

and the javascript.

<script type="text/javascript" defer="defer">
function google_click(width, height)
{
    var leftPosition, topPosition;
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);
    var windowFeatures = "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no";
    var u=location.href;
    var t=document.title;
    window.open('https://plus.google.com/share?url='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer', windowFeatures);
    return false;
}
</script>

Upvotes: 5

Related Questions