10 09/2014

stackoverflow hot tags

最后更新: Wed Sep 10 2014 12:35:31 GMT+0800

php xml 2 json code

js 没啥,主要是 php 抓 feed 转 jsonp

<?php 
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");

$tag=$_GET['tag'];

$ch=curl_init('http://stackoverflow.com/feeds/tag/'.$tag);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回字符串,而不是直接输出
$data=curl_exec($ch);

// $fileContents = str_replace(array("\n", "\r", "\t"), '', $data);
$fileContents=$data;
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);

echo $_GET['callback'] . '('.$json.')';
?>