博主使用php发帖
Posted
技术标签:
【中文标题】博主使用php发帖【英文标题】:blogger posting using php 【发布时间】:2015-05-04 03:56:50 【问题描述】:我的一个朋友给了我这个使用 php 发布博客的代码。但它不起作用。我的服务器已经有 cURL
<?php session_start();
$email = "blogger_email@gmail.com";
$pass = "password";
$blogID= urlencode("blogger_id"); // like 6304924319904337556
// Do Not Modify Below Code
if(!isset($_SESSION['sessionToken']))
$ch = curl_init("https://www.google.com/accounts/ClientLogin?Email=$email&Passwd=$pass&service=blogger&accountType=GOOGLE");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
curl_close($ch);
$arr = explode("=",$result);
$token = $arr[3];
$_SESSION['sessionToken'] = $token;
$entry = "<entry xmlns='http://www.w3.org/2005/Atom'>
<title type='text'>Title of blog post </title>
<content type='xhtml'>
This is testing contnetto post in blog post.
</content>
</entry>";
$len = strlen($entry);
$headers = array("Content-type: application/atom+xml","Content-Length: $len","Authorization: GoogleLogin auth=$_SESSION['sessionToken']","$entry");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.blogger.com/feeds/$blogID/posts/default");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
$ERROR_CODE = curl_getinfo($ch);
curl_close($ch);
echo '<pre>';
print_r($headers);
var_dump($result);
print_r($ERROR_CODE);
exit;
?>
当我运行这段代码时。得到这样的结果
数组 ( [0] => 内容类型:application/atom+xml [1] => 内容长度:208 [2] => 授权:GoogleLogin auth= [3] =>
This is testing contnetto post in blog post.
) 布尔(假) 大批 ( [网址] => https://www.blogger.com/feeds/7493633362314585130/posts/default [内容类型] => [http_code] => 0 [header_size] => 0 [请求大小] => 417 [文件时间] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [总时间] => 4.001117 [namelookup_time] => 0.001279 [连接时间] => 0.007472 [pretransfer_time] => 0.026912 [大小上传] => 0 [大小下载] => 0 [速度下载] => 0 [速度上传] => 0 [下载内容长度] => -1 [上传内容长度] => -1 [starttransfer_time] => 1.028038 [重定向时间] => 0 [certinfo] => 数组 ( )
[redirect_url] =>
)
博主不会摆出任何姿势。你能帮我修复这个代码吗
对不起我的英语
【问题讨论】:
【参考方案1】:尝试使用PHPMailer 解释 here 这也可用于使用 php 发送到您博客上包含您的密钥的电子邮件地址的博客发帖。代码如下:
include('class.phpmailer.php');
$gmail_your_name = "YOUR NAME";
$gmail_username = "YOUR GMAIL USERNAME";
$gmail_password = "YOUR GMAIL PASSWORD";
$gmail_email = "YOUR GMAIL EMAIL ADDRESS";
$blogger_secret_key = "YOUR BLOGGER SECRET KEY";
$blogger_url = "YOUR BLOGGER URL";
$image_location = 'C:/YOUR LOCATION OF IMAGE/IMAGE.JPG';
$email_title = "EMAIL TITLE";
$email_body = "EMAIL BODY"; // HTML OK
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->;SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = $gmail_username;
$mail->Password = $gmail_password;
$fromname = $gmail_your_name;
$To = trim($gmail_username.'.'.$blogger_secret_key.'@blogger.com',"\r\n");
$mail->AddAttachment($image_location);
$mail->From = $gmail_email;
$mail->FromName = $fromname;
$mail->Subject = $email_title;
$mail->Body = $email_body;
$mail->AddAddress($To);
$mail->set('X-Priority', '3'); //Priority 1 = High, 3 = Normal, 5 = low
$mail->Send();
【讨论】:
以上是关于博主使用php发帖的主要内容,如果未能解决你的问题,请参考以下文章