我正在尝试为我的网站构建一个宁静的Web服务。我有一个使用以下代码的php mysql查询:
function mysql_fetch_rowsarr($result, $taskId, $num, $count){
$got = array();
if(mysql_num_rows($result) == 0)
return $got;
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
$got[]=$row;
}
print_r($row)
print_r(json_encode($result));
return $got;它使用上面代码中的print_r($ data)返回以下内容
Array ( [0] => Array ( [show] => Blip TV Photoshop Users TV [region] => UK [url] => http://blip.tv/photoshop-user-tv/rss [resourceType] => RSS / Atom feed [plugin] => Blip TV ) [1] => Array ( [show] => TV Highlights [region] => UK [url] => http://feeds.bbc.co.uk/iplayer/highlights/tv [resourceType] => RSS / Atom feed [plugin] => iPlayer (UK) ) )这是它返回的json:
[{"show":"Blip TV Photoshop Users TV","region":"UK","url":"http:\/\/blip.tv\/photoshop-user-tv\/rss","resourceType":"RSS \/ Atom feed","plugin":"Blip TV"},{"show":"TV Highlights","region":"UK","url":"http:\/\/feeds.bbc.co.uk\/iplayer\/highlights\/tv","resourceType":"RSS \/ Atom feed","plugin":"iPlayer (UK)"}]我使用下面的代码将一些项目添加到数组然后将其转换为json并返回json。
$got=array(array("resource"=>$taskId,"requestedSize"=>$num,"totalSize"=>$count,"items"),$got);使用下面的代码将其转换为json并将其返回。
$response->body = json_encode($result);
return $response;这给了我下面的json。
[{"resource":"video","requestedSize":2,"totalSize":61,"0":"items"},[{"show":"Blip TV Photoshop Users TV","region":"UK","url":"http:\/\/blip.tv\/photoshop-user-tv\/rss","resourceType":"RSS \/ Atom feed","plugin":"Blip TV"},{"show":"TV Highlights","region":"UK","url":"http:\/\/feeds.bbc.co.uk\/iplayer\/highlights\/tv","resourceType":"RSS \/ Atom feed","plugin":"iPlayer (UK)"}]]API的使用者需要以下格式的json,我无法弄清楚如何使它以这种方式出现。我搜索并尝试了所有我能找到的东西,但仍然没有得到它。而我甚至没有开始尝试获取XML格式
{"resource":"video", "returnedSize":2, "totalSize":60,"items":[{"show":"Blip TV Photoshop Users TV","region":"UK","url":"http://blip.tv/photoshop-user-tv/rss","resourceType":"RSS / Atom feed","plugin":"Blip TV"},{"show":"TV Highlights","region":"UK", "url":"http://feeds.bbc.co.uk/iplayer/highlights/tv","resourceType":"RSS / Atom feed","plugin":"iPlayer (UK)"}]}我非常感谢所有帮助。我已经设置了只读访问数据库的副本,可以给所有的源代码,它会帮助,我会警告你,我只是现在学习PHP,我学会了在基本,fortran 77编程,所以PHP是相当凌乱我猜会很臃肿。
OK上面关于json编码被回答了。 API消费者也需要特殊字符“/”,因为它是一个URL,所以不会被转义。我尝试了json_encode中的“JSON_UNESCAPED_SLASHES”,并得到以下错误。
json_encode() expects parameter 2 to be long