10
09/2014
php create jsonp
jsonp 返回的是
// Response: jsonp1277656587731([1,2,3,4,5,6,7,8,9])
<?php header('content-type: application/json; charset=utf-8'); header("access-control-allow-origin: *"); $cid=$_GET['cid']; $ch=curl_init('http://m.weather.com.cn/data/'.$cid.'.html'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回字符串,而不是直接输出 $data=curl_exec($ch); echo $_GET['callback'] . '('.$data.')'; ?>
如果需要同时返回 json 和 jsonp 的话
<?php header('content-type: application/json; charset=utf-8'); $data = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $json = json_encode($data); echo isset($_GET['callback']) ? "{$_GET['callback']}($json)" : $json;
via http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/