Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to build a restful web service for my website. I have a php mysql query using the following code:

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;  

which returns the following using the print_r($data) in the code above

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) ) ) 

Here is the json it returns:

 [{"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)"}]

I am using the following code to add some items to the array then convert it to json and return the json.

 $got=array(array("resource"=>$taskId,"requestedSize"=>$num,"totalSize"=>$count,"items"),$got);

using the following code to convert it to json and return it.

 $response->body = json_encode($result);
 return $response;

this gives me the following 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)"}]]

The consumers of the API want the json in the following format and I cannot figure out how to get it to come out this way. I have searched and tried everything I can find and still not get it. And I have not even started trying to get the xml formatting

 {"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)"}]}

I appreciate any and all help with this. I have setup a copy of the database with readonly access and can give all the source code it that will help, I will warn you that I am just now learning php, I learned to program in basic, fortran 77 so the php is pretty messy and I would guess pretty bloated.

OK The above about json encoding was answered. The API consumers also want the special character "/", not to be escaped since it is a URL. I tried the "JSON_UNESCAPED_SLASHES " in the json_encode and got the following error.

 json_encode() expects parameter 2 to be long
share|improve this question
Apply json_decode to the desired json output and see what array you need to assemble to get the same – zerkms Apr 11 '12 at 23:47
how can I get the desired json into php? can I make it a string then json)decode the string? – Moltra Apr 11 '12 at 23:48
"The consumers of the API want the json in the following format " --- this is the desired format, the following one. The one your consumers want is obviously a desired – zerkms Apr 11 '12 at 23:49

1 Answer

up vote 0 down vote accepted

Your $result line should look like

$result=array(
    "resource"=>$taskId,
    "requestedSize"=>$num,
    "totalSize"=>$count,
    "items" => $got
);
share|improve this answer
funny how easy it is once someone puts the correct answer under your nose. – Moltra Apr 11 '12 at 23:51
You would get the same if you used json_decode with the line you want to achieve (the last json in your question) – zerkms Apr 11 '12 at 23:53
I made a small change to the question, I tried to add the JSON_UNESCAPED_SLASHES to the json_encode and get the following. Warning: json_encode() expects parameter 2 to be long, string given in – Moltra Apr 11 '12 at 23:59
@Moltra: as it said in documentation (nz.php.net/json_encode) that constant was introduced in php 5.4. Obviously you use some older version – zerkms Apr 12 '12 at 0:05
Yes I am using PHP Version: PHP 5.3 Is there anyway around this using php 5.3 that is the current php on godaddy 4GH. – Moltra Apr 12 '12 at 0:07
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.