本サイト/記事は移転しました。

約10秒後にリダイレクトします。

PHPではてなハイクに画像添付投稿する

 昨日の続き。

 できた。参考にしたのは以下のページ。
PHPのHTTP_RequestでGETとかPOSTとかファイルのアップロードとかクッキーとかやってみたよ - sdkt4aの日記

<?php
function write_haiku($keyword,$body,$file=""){
    global $username,$password;
    $body     = mb_convert_encoding($body,'UTF-8','auto');
    $url     = 'http://h.hatena.ne.jp/api/statuses/update.xml?';

    $req =& new HTTP_Request($url);
    $req->setMethod(HTTP_REQUEST_METHOD_POST);
    $req->addHeader("Authorization",'Basic '. base64_encode($username. ':'. $password));
    $req->addPostData("keyword", $keyword);
    $req->addPostData("status", $body);
    if ($file!="") {$req->addFile("file", $file, "Content-Type: image/jpeg");}
    $req->sendRequest();
    $result = $req->getResponseBody();

    echo $result;
}
?>

 エラー処理とかかなりいいかげん。

 返ってきたのはこんなxml

<?xml version='1.0' standalone='yes'?>
<status>
  <id>9259269496029230333</id>
  <created_at>2010-05-12T12:39:00Z</created_at>
  <favorited>0</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <keyword>ひとりごと</keyword>
  <link>http://h.hatena.ne.jp/doroyamada/9259269496029230333</link>
  <source>API</source>
  <text>ひとりごと=テストです。
http://f.hatena.ne.jp/images/fotolife/d/doroyamada/20060810/20060810153358.jpg
</text>
  <user>
    <name>doroyamada</name>
    <followers_count>3</followers_count>
    <id>doroyamada</id>
    <profile_image_url>http://www.st-hatena.com/users/do/doroyamada/profile.gif</profile_image_url>
    <screen_name>doroyamada</screen_name>
    <url>http://h.hatena.ne.jp/doroyamada/</url>
  </user>
</status>

 ありゃ、添付ファイルのはてなフォトライフ内でのurlは直接の要素で返ってこないんやね。<text>要素の中から引き出すしかないのか。あまりxml的ちゃうね。