How to display Now Playing in my website with Icecast2 and Liquidsoap - php

I couldn't find any better answer to this online, even the document of liquidsoap isn't helpful. What I want to happen is to grab the current song title and artist being played in my streaming server(icecast). I found in some forum that they were able to do it but they didn't explain it how, here's the liquidsoap script that they used:
def apply_metadata(m) =
title = m["title"]
artist = m["artist"]
album = m["album"]
[("artist","#{artist}"),("title","#{album} - #{title}")]
end
centovacast.callback_autodj := fun(s) -> map_metadata(apply_metadata,s)
This script i believe is also for centova and autodj only. While I do not use those technology (I'm using Ubuntu 16.04, Icecast2, Liquisoap, PHP, HTML5/CSS),
is this possible to do using the tools I'm currently using?

I used to use websockets to get the metadata but I found it frustrating that it was always out of sync.
The only way to solve it is to have the metadata encoded into the stream so you receive it at the same time as the audio.
I did a bit of digging around to find out how the icecast servers do it and put together a service worker script which adds the necessary header to your request to obtain a stream including metadata and then extracts it for you.
The code is here and there is a simple demo here

I hope this helps, anyway, I think that here, weserWEB, has already somehow said something similar.
If you're using Icecast version 2.4.4 you can consume the metadata from this endpoint: (if not, consider downloading that version)
http://<your-ipaddress-or-domain>:<port_number>/status-json.xsl
Just put that in any browser and you will get a JSON with the name of the song and title you are streaming, you have to configure properly your streaming client first, of course.
Then you can get the metadata from that endpoint without problems in your PHP. You can use CURL to get the JSON data, liquidsoap not necessary.

I'm not sure why you are dragging the source client into this.
A proper stream sent to an Icecast mountpoint will have metadata for currently playing audio.
This has been pointed out elsewhere. Icecast since 2.4.1 provides a proper JSON metadata export.
Querying JSON from within a website is very much a solved problem and considered an exercise for the inclined reader.

Why don't you grab this at icecast directly?
PHP:
function get_icecast_info($server_ip, $server_port, $admin_user, $admin_password) {
$index = #file_get_contents("http://".$admin_user.":".$admin_password."#".$server_ip.":".$server_port."/admin/stats.xml");
if($index) {
$xml = new DOMDocument(); if(!$xml->loadXML($index)) return false; $arr = array(); $listItem = $xml->getElementsByTagName("source");
foreach($listItem as $element) {
if($element->childNodes->length) {
foreach($element->childNodes as $i){ $arr[$element->getAttribute("mount")][$i->nodeName] = $i->nodeValue; }
}
}
return $arr;
} return false;
}
And this is the output (array):
$arr = get_icecast_info($ice_host, $ice_aport, $ice_user, $ice_pass);

Related

Updating documents with the help of cas in couchbase 2.0 server using PHP API

I am trying to update a document in Couchbase 2.0 server using PHP API (php-ext-couchbase).
http://www.couchbase.com/docs/couchbase-sdk-php-1.1/api-reference-summary.html
The document is similar to facebook POSTS with comments and likes associated with it.
To be more specific.
-Load the doc.
-Modify it
-Store the modified doc if it has not been accessed by any other person.
Basic operations that would be required to accomplish this will be
# Get a document by key
doc = get(key)
# Modify a document when no one has modified it since my last read
casVersion = doc.getCas()
cas(key, casVersion, changedDoc)
I just want to know how i can accomplish this in PHP.
especially how to get the casVersion or the revision_id of the document and then further carry out the update process so that changes made by simultaneous updates on same document are not lost.
No worries I found it,
The code is
$bucket='yourbucketname';
$cb=new Couchbase("127.0.0.1:8091","root","password",$bucket);
$old_doc=null;
$cb->getDelayed($obj_id, true,
function($cb, $data) use (&$old_doc) { $old_doc = $data; });
$casKey=$old_doc['cas'];
Use getDelayed method, and callback function to get the array having (key,value,caskey)
I hope the couchbase documentation would be more clear in future with usages examples.

Solr for PHP: getDigestedResponse not working

I've managed to install Solr for PHP on my Windows 7 64bit Machine using the plugin I found here:
downloads.php.net/pierre/
It was linked to on this site:
wiki.apache.org/solr/SolPHP
(links are not clickable because I'm a new user)
I've got everything up and running, searches and indexing are working, but only when I use the getRawResponse() Method and parse it through SimpleXml (http://de.php.net/manual/en/book.simplexml.php).
The getDigestedResponse() method, which ist supposed to return a PHP-Object, just returns string(1) " ".
The method getResponse() (http://docs.php.net/manual/en/solrresponse.getresponse.php) just times out.
It wouldn't be that much of a problem, but some of the XML from the Raw Response doesn't seem to be valid and parsed with simpleXML, some of the attributes are missing, using regular expressions to get the needed data would be too much of a hassle.
Has anyone get this to work yet? Help is greatly appreciated!
Depends on how you are parsing the response. Try code below and drop the PHP/PECL solr libs and go CURL (ex: hostNameHere:8983/solr/select/?q=solr&start=0&rows=10&indent=on and send the result XML to the function below).
If you can access a resource (solr) via a URL, then there is no need to use an ancillary library to do what CURL can do:
function makeSimpleXML($xml) {
$dom = new DOMDocument;
$dom->loadXML($xml);
if (!$dom) {
// ErrorUtility::throwFatal("could not parse xml. please check the format", "XMLParisng Error");
}
return simplexml_import_dom($dom);
}

Ajax from within PHP

I know AJAX will not be what it is called, but I am looking for something similar that can be done from within PHP itself (not using javascript).
Basically, as the PHP is creating the page, I want to query an API to gather some information that will be used on the current page. Is this possible, and if so what would be the best method?
Thanks
You can use fopen or curl:
Here is an example to open a connection to twitters API, read the public timeline and output parts of it.
<?php
$fp = fopen("http://api.twitter.com/1/statuses/public_timeline.json?count=3&include_entities=false","r");
while($data = fgets($fp))
{
$json .= $data;
}
$arr = json_decode($json);
print_r($arr);
?>
You are probably wanting curl.
http://php.net/manual/en/book.curl.php
If you're talking about a remote HTTP API, you would utilize cURL at runtime. It's basically PHP's way of accessing information across domains.
The above can be a little overwhelming - check out the simple example page to get you started. If available, I suggest working with cURL from the command line, as there's quite a few options on there. It's a good way to get familiar with the command and different options available.

Parse Google Images API Json PHP

Hey, well I'm trying to use google images api with PHP, and I'm really not sure what to do.
This is basically what I have right now:
$jsonurl = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=test";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
Where would I go from there to retrieve the first image url?
With a minor change to the last line of your code sample, the following will output the url of the first image in the result set.
<?php
$jsrc = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=test";
$json = file_get_contents($jsrc);
$jset = json_decode($json, true);
echo $jset["responseData"]["results"][0]["url"];
?>
For security reasons, most server configurations won't let you use file_get_contents on a remote file (different domain name). It would potentially allow a hacker to load code from anywhere on the Internet to your site, then execute it.
Even if your server configuration does allow for it, then I wouldn't recommend using it for this purpose. The standard tool for retrieving remote HTTP data is cURL, and there are plenty of good tutorials out there doing exactly what you should do in this case.
So, let's say you've successfully used cURL to retrieve the JSON array.
$json_output = json_decode($json); // Now the JSON is an associative array
foreach ($json_output['responseData']['results'] as $result)
{
echo $result['url'] . '<br />';
}
Of course, you don't have to echo the URL there; you can do whatever you need to with the value.
I have to say, this is 10 shades of awesome.. But I come with bad news (don't shoot the messenger..)
Important: The Google Image Search API has been officially deprecated as of May 26, 2011. It will continue to work as per our deprecation policy, but the number of requests you may make per day may be limited.
That is, as they same, lame.
I feel as if Google might have hired one-too-many layed-off-from-IBM-types... as they seem to be killing off all their "cool" API's.
They launch services haphazardly, promising this and that and the other thing... but then some middle-manager gets screamed at after realizing (ta-da!) that XYZ project doesn't generate income (like image results without ads, lol) and then... they axe it..
Lesson: Don't get married (aka build your software or service) around any google API you can't replace on-the-fly at a moment's notice... Now, I'm no LTS-junkie - I'm just bitter because I'd much rather get my Google results via XML or JSON than the icky HTML-soup they throw at you...
One Question #Marcel... How can I get an array, or at least multiple JSON result responses using that same "formula". I thought maybe the 1 meant "result 1", but alas, no... Is their a "trick" to generate a content stream ala a Picasa xml feed?

Mediawiki + AJAX + IE = broken

I have created an extension for mediawiki that works in all major browsers other than IE (any version it appears). The extension relies on mediawiki's ajax wrapper to send an xmlhttprequest with parameters that essentially build a database query to a php script. This script will run a query based on the parameters and then create an XML object (using php's simplexml class) which then returns the XML to javascript for display in the browser (just a table, mostly).
Now with all that information, IE seems to be working up until the point at which it tries to parse the returned XML. I have set the mime type to application/xml and I have tried loading it with various different techniques found via google (none worked).
It is trivial to load the XML for parsing when using non-IE browsers:
function callbackHCL(response){
if (response.readyState == 4) {
var xmlObj = response.responseXML;
if (response.status == '200'){
if (xmlObj !== undefined){
//etc...
Now I can start using dom functions to get at data.
My Question: Does anybody have any suggestions on how to parse xml in IE based on my current scenario?
If you would like to email me at tccroninv#gmail.com, I can provide longer code snippets, they are longer and I don't believe they would help the situation. If you would like me to post more code, just ask as well.
Thanks in advance,
Tim
I think this might be what you want: http://dean.edwards.name/weblog/2006/04/easy-xml/. Basically, IE doesn't return an XML document like the other guys. Need to do a little fancy footwork to make it work correctly. I'm sure there's a library out there that wraps this all up so you don't have to worry about it if you don't want to.

Categories