博客发布使用PHP

时间:2015-03-03 13:17:56

标签: php blogger

我的一位朋友给了我这个使用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] =&gt;内容类型:application / atom + xml       [1] =&gt;内容长度:208       [2] =&gt;授权:GoogleLogin auth =       [3] =&gt;

This is testing contnetto post in blog post.

) 布尔(假) 排列 (     [url] =&gt; https://www.blogger.com/feeds/7493633362314585130/posts/default     [content_type] =&gt;     [http_code] =&gt; 0     [header_size] =&gt; 0     [request_size] =&gt; 417     [filetime] =&gt; -1     [ssl_verify_result] =&gt; 0     [redirect_count] =&gt; 0     [total_time] =&gt; 4.001117     [namelookup_time] =&gt; 0.001279     [connect_time] =&gt; 0.007472     [pretransfer_time] =&gt; 0.026912     [size_upload] =&gt; 0     [size_download] =&gt; 0     [speed_download] =&gt; 0     [speed_upload] =&gt; 0     [download_content_length] =&gt; -1     [upload_content_length] =&gt; -1     [starttransfer_time] =&gt; 1.028038     [redirect_time] =&gt; 0     [certinfo] =&gt;排列         (         )

[redirect_url] => 

博客不会冒充任何东西。你能帮我解决这个问题吗?

抱歉我的英文

1 个答案:

答案 0 :(得分:0)

尝试使用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();