【php】更新Pingをphpで送信する(weblogUpdates.ping 引数3つ)
問題
phpで、サイトの更新pingをさくっと送りたい。
回答例
サイトの更新pingを送信するとは、Pingサーバーに、POSTリクエストで、XML-RPCのメッセージを送ること。
phpはfile_get_contents()関数でPOSTリクエストを送信できる。とてもお手軽。
weblogUpdates.ping の詳細は、こちら参照 http://www.xmlrpc.com/weblogsCom
weblogUpdates.ping (weblogname, weblogurl, changesurl=weblogurl, categoryname="none")
ということだそうなので、そのようにデータを送る。
//更新Pingの送信先
$server = 'http://pingサーバーを指定/';
//weblogUpdates.ping のXML-RPCのリクエストを作る
$content = xmlrpc_encode_request(
'weblogUpdates.ping',
// 4つ目の引数の"カテゴリ"は省略してよい
array('ブログの名前', 'http://ブログのURL', '更新のあったURL'),
array('encoding' => 'UTF-8')
);
//HTTPコンテキスト [http://www.php.net/manual/ja/context.http.php] 参照
$options = array('http'=>array(
'method' => 'POST',
'header' => 'Content-type: text/xml' . "\r\n"
. 'Content-length: ' . strlen($content),
'content' => $content
));
$context = stream_context_create($options);
//リクエスト送信
$response = file_get_contents($server, false, $context);
こんなリクエスト(XML)が送信される。
<?xml version="1.0" encoding="UTF-8"?> <methodCall> <methodName>weblogUpdates.ping</methodName> <params> <param> <value> <string>xxxxxxxxxx</string> </value> </param> <param> <value> <string>http://www.example.com/</string> </value> </param> <param> <value> <string>http://www.example.com/abc/123</string> </value> </param> </params> </methodCall>
うまくいくとレスポンスはこんな感じ。
HTTP/1.1 200 OK Connection: close Content-Length: 333 Content-Type: text/xml Date: Sun, 30 Sep 2001 20:02:30 GMT Server: UserLand Frontier/7.0.1-WinNT <?xml version="1.0"?> <methodResponse> <params> <param> <value> <struct> <member> <name>flerror</name> <value> <boolean>0</boolean> </value> </member> <member> <name>message</name> <value>Thanks for the ping.</value> </member> </struct> </value> </param> </params> </methodResponse>