I am setting up a music recognition app on my website. I have a streaming radio running on my website and I want to show the title and artist of each song on the radio page. I am using arcCloud's broadcast monitoring custom stream. I am able to get the title and artist to show up on the radio page, but only if the radio page refreshed. I want the title and artist to show up with out having to refresh the page.
I have tried running a cron job to run the php script every 60 seconds and the script will run, but it will not change the information on the page. Unless the page is refreshed.
I have checked that the stream is running and that all of the info is correct.
I have been trying to set up a callback URL - so I can either write the data to .txt file or add the data to database, but I can not seem to get the callback to update either. I can get the song to write to the data.tx file if i refresh the page. I have tried using an ajax call but because my radio is streamed from an IP that is HTTP and not HTTPS I get a cross origin error.
Using a XMLHttpRequest I can get the data from the data.tx file to print on the radio page - every 30 seconds, but I can not get the callback to deliver the data with out refreshing the page.
I think the best solution is to have the callback url write the data on to my data.tx and have the XMLHttpRequest write it onto the page every 30 seconds. So I need help with my callback URL - I need it to update the data.txt file each time a new song plays, I do not need it to add to my data.txt file I want it to overwrite so only one song and title are on the data.txt at a time.
script.php
<?php
$json_string = 'https://api.acrcloud.com/v1/monitor-streams/s-
xxxxxxxxx/results?access_key=xxxxxxxxxxxxxxx';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
print_r($obj[0]['metadata']['music'][0]['title']);
?>
<?php
$json_string = 'https://api.acrcloud.com/v1/monitor-streams/s-
xxxxxxxx/results?access_key=xxxxxxxxxxx';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
print_r($obj[0]['metadata']['music'][0]['artists'][0]['name']);
?>
This prints the Artist and Song Title - but only if page is loaded.
<?php
$dir = "data.txt";
if(!is_writable($dir)){
echo "cannot write to file";
}
if (!empty($_REQUEST["search"])) {
$input = $_REQUEST["search"];
$bitecount = file_put_contents($dir, $input, FILE_APPEND);
}
echo $bitecount . " bites were written to the file";
?>
This will print the data from the API - but does not update.
test7.html
function checkData() {
$.ajax({
type: "GET",
url: "https://api.acrcloud.com/v1/monitor-streams/s-
xxxxxxx/results?access_key=xxxxxxx",
success: function (data) {
console.log(data);
}
});
}
setInterval(checkData, 30000);
</script>
This gives me a CORS Error: Access to XMLHttpRequest at 'https://api.acrcloud.com/v1/monitor-streams/s-DN5fdGT/results?access_key=xxxx' from origin 'https://saltydog.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I am a very beginner developer and I have no experience with API's and very little with php so I do not understand how to write or execute the callback URL for arccloud. I understand what a callback does but not how to write one to get the info I need. Their documentation reads:
You can set Callback URL for monitoring results by the following steps: select the project under Custom Streams Monitoring projects then click “Action” —> click “Set Result Callback” in the dropdown menu —> input the callback URL and click “Update”.
I set this as my callback URL (https://saltydog.com/radio/data.txt) and when I run their test it reads success but nothing gets written to my data.txt.
I don't know what this URL is suppose to look like? How does this work? https://www.acrcloud.com/docs/acrcloud/audio-fingerprinting-api/monitoring-api-2/monitoring-api/
When I contact their support they keep sending me tutorials and documentation I have already read.
Continuation of their documentation: You will post “stream_id”, “stream_url”,”status” and monitoring results “data” (json format) to the callback URL
#For JSON Data
$filename = "https://saltydog.com/radio/data.txt";
$data = file_get_contents('php://input');
file_put_contents($filename, $data."
", FILE_APPEND);
$json_data = json_decode($data, true);
$stream_id = $json_data["stream_id"];
$stream_url = $json_data["stream_url"];
$stream_result = $json_data["data"]; #monitor result body.
$stream_status = $json_data["status"]; #status=0 means data is
No Result.
#do something...
echo "OK";
?>
But I just don't know what I am supposed to do, when I add the above code to my script.php and run it it echo's "OK" but I don't understand how to write to the data.tx file, Shouldn't file_put_contents write the data to the file? Or do i need to $post = "something"? I have tried several variations of $post to write to the data.txt file. I am very lost.