Hi Dimitri.
I had some issues when reading API response format from a website. Because some are in XML format and others are in plain text, Javascript can't handle both formats in the same function, as it stops if an error is found.
So, I came up with a simple solution:
1) Passing all the requestes through a route PHP script that receives the URL command, checks if it is ok, and then sends it to RB port.
2) RB sends back the response either in XML or Text. The route script looks for the XML tag, and if there isn't any it's a text response.
3) A browser header is set according to the received format and the response is sent to the website.
This solution is perfect and solves 2 major problems: Using SSL protocol with the request, and formating the output header.
But this isn't a solution to everyone. The header issue is important to fix, so it can be read by javascript without errors by any simple script.
The TODO for RB:
Setting header params for the response before it is sent, the same way you do to files.
The workarround PHP code:
-------------------------------------------------------------------
function doTask($api,$cmd)
{
$handle = fopen($api.$cmd, "rb");
$resp = stream_get_contents($handle);
fclose($handle);
return $resp;
}
$xml = doTask($api,"?".$action.$pwd);
if ( strpos($xml,"?xml") ) {
header("Content-type: text/xml; charset=utf-8");
} else {
header("Content-type: text/html; charset=utf-8");
}
echo $xml;
-------------------------------------------------------------------
If anyone else is having this issue, this might be useful.
Other stuff:
Dimitri, you're doing a great job. API is a great tool! I managed to control RB from a website, including playlist managing, setting sheduler events, play requests, and performimg live shows from remote locations without any attendant at RB computer.
I'm having so much fun coding all this features to the website. I don't need to be at RB computer. Only need a browser to run the radiostation.
If you need more info on this, PM me.
Regards.
I had some issues when reading API response format from a website. Because some are in XML format and others are in plain text, Javascript can't handle both formats in the same function, as it stops if an error is found.
So, I came up with a simple solution:
1) Passing all the requestes through a route PHP script that receives the URL command, checks if it is ok, and then sends it to RB port.
2) RB sends back the response either in XML or Text. The route script looks for the XML tag, and if there isn't any it's a text response.
3) A browser header is set according to the received format and the response is sent to the website.
This solution is perfect and solves 2 major problems: Using SSL protocol with the request, and formating the output header.
But this isn't a solution to everyone. The header issue is important to fix, so it can be read by javascript without errors by any simple script.
The TODO for RB:
Setting header params for the response before it is sent, the same way you do to files.
The workarround PHP code:
-------------------------------------------------------------------
function doTask($api,$cmd)
{
$handle = fopen($api.$cmd, "rb");
$resp = stream_get_contents($handle);
fclose($handle);
return $resp;
}
$xml = doTask($api,"?".$action.$pwd);
if ( strpos($xml,"?xml") ) {
header("Content-type: text/xml; charset=utf-8");
} else {
header("Content-type: text/html; charset=utf-8");
}
echo $xml;
-------------------------------------------------------------------
If anyone else is having this issue, this might be useful.
Other stuff:
Dimitri, you're doing a great job. API is a great tool! I managed to control RB from a website, including playlist managing, setting sheduler events, play requests, and performimg live shows from remote locations without any attendant at RB computer.
I'm having so much fun coding all this features to the website. I don't need to be at RB computer. Only need a browser to run the radiostation.
If you need more info on this, PM me.
Regards.