使用PHP Curl在Google Plus上发布状态

时间:2013-12-06 05:06:05

标签: php curl google-plus

我有使用简单DOM解析器和curl登录Google的代码。我尝试使用以下代码在google plus上发布状态但无法在google plus上发布

关于如何解决这个问题的任何想法?

这是我的参考代码:

$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
    "accountType" => "HOSTED_OR_GOOGLE",
    "Email" => "youremail@gmail.com",
    "Passwd" => "yourpassword",
    "service" => "writely",
    "source" => "your application name"
  );

// Initialize the curl object
$curl = curl_init($clientlogin_url);

// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// Execute
$response = curl_exec($curl);

// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];

$params['newcontent'] = "Post on Google Plus Test By Me";

$headers = array(
    "Authorization: GoogleLogin auth=" . $auth,
    "GData-Version: 3.0",
);

//  Make the request
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

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

感谢您的帮助和合作

3 个答案:

答案 0 :(得分:2)

没有Google+ API可以发布状态(除非您是Google Apps user)和ClientLogin has been deprecated,并且很快就会停止投放。你最好的选择是使用像share plugin这样的东西。

答案 1 :(得分:1)

为了不显示已移动的内容,您必须设置代理标头,如firefox或其他内容 示例

curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl,"USER_AGENT","firefox my browser");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect

 $response = curl_exec($curl);
 var_dump($response);// Added here
  curl_close($curl);`enter code here`

答案 2 :(得分:0)

您也需要在这里添加这两个 cURL 参数。

curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect

此外,您没有在代码上输出任何回复

$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);