<?php
ini_set("include_path", "/home/sites/heteml/users11/c/a/m/camelrider/web/gian/pear" . PATH_SEPARATOR . ini_get("include_path") );
require "RPC.php";

ini_set("error_reporting",0);

function error($msg) {
	$xmldata = "";
	$xmldata .= '<?xml version="1.0" encoding="UTF-8"?>';
	$xmldata .= "<user>";
	$xmldata .= "<returnCode>NG</returnCode>";
	$xmldata .= "<msg>".$msg."</msg>";
	$xmldata .= "</user>";
	echo $xmldata;
	exit(0);
}

$user = $_REQUEST["user"];
//$user = " cmrr_xxx";

//twitterユーザ情報取得
$req = "http://twitter.com/users/show/".$user.".xml";

header("Content-Type: text/plain; charset=utf-8");

$xmlTwitter = file_get_contents($req)
or error("twitter error¥nIDが間違っている可能性があります");

$xmlPT = simplexml_load_string($xmlTwitter)
or error("twitter error¥nパースエラーです");
$url = $xmlPT->url;
$followers_count = $xmlPT->followers_count;
$statuses_count = $xmlPT->statuses_count;
$hit_rate = $followers_count / $statuses_count;
if($url==""){
	$scouterValue = "0";
	$hatenaTotalCount = "0";
}
else{
	//webscouter取得
	$req2 = "http://ja.webscouter.net/api.phtml?ver=2&u=".$url;
	$xmlScouter = file_get_contents($req2)
	or error("webscouter error¥nURLが間違っている可能性があります");

	$xmlPS = simplexml_load_string($xmlScouter)
	or error("webscouter error¥nパースエラーです");

	$scouterValue = $xmlPS->scouterValue;

	//はてなサイトブックマーク数取得
	$params = array(new XML_RPC_Value($url, 'string'));
	$msg = new XML_RPC_Message('bookmark.getTotalCount', $params);
	$cli = new XML_RPC_Client('/xmlrpc', 'b.hatena.ne.jp');
	$resp = $cli->send($msg);
	if (!$resp) {
		error("hatena api error1¥n".$cli->errstr);
	}

	if(!$resp->faultCode()){
		$val = $resp->value();
		$data = XML_RPC_decode($val);
		//var_dump($data);
		$hatenaTotalCount = $data;
	}else{
		error("hatena api error2¥n".$resp->faultCode().":".$resp->faultString());
	}
}

$xmldata = "";
$xmldata .= '<?xml version="1.0" encoding="UTF-8"?>';
$xmldata .= "<user>";
$xmldata .= "<returnCode>OK</returnCode>";
$xmldata .= "<followers_count>".$followers_count."</followers_count>";
$xmldata .= "<statuses_count>".$statuses_count."</statuses_count>";
$xmldata .= "<hit_rate>".$hit_rate."</hit_rate>";
$xmldata .= "<scouterValue>".$scouterValue."</scouterValue>";
$xmldata .= "<hatenaTotalCount>".$hatenaTotalCount."</hatenaTotalCount>";
$xmldata .= "</user>";
echo $xmldata;
?>