Sinisterly
Biggest PHP noob ever - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: PHP (https://sinister.li/Forum-PHP)
+--- Thread: Biggest PHP noob ever (/Thread-Biggest-PHP-noob-ever)



Biggest PHP noob ever - Rick - 12-19-2015

Hey lads, I've literally never touched anything web-based stuff and I'm feeling pretty stumped.

So I've been setting something up on a Raspi that steals a youtube mp3 and plays it down the aux. The only problem I have is going about getting requests from users. I know what to do once I receive a URL, but I don't know how to receive it.. D'ya get me?

Ideally I'd like to just have a way for the Pi to recieve requests. I've never done anything transmission based so don't have a clue.

I'm asking here because ideally I'd like to set up a PHP page (Can be hosted on the pi itself on a LAN) with only a box to input the URL and a "send request" button.

Any help at all would be greatly appreciated.


RE: Biggest PHP noob ever - Megan - 12-19-2015

(12-19-2015, 12:24 AM)Rick Wrote: Hey lads and ladies, I've literally never touched anything web-based stuff and I'm feeling pretty stumped.

So I've been setting something up on a Raspi that steals a youtube mp3 and plays it down the aux. The only problem I have is going about getting requests from users. I know what to do once I receive a URL, but I don't know how to receive it.. D'ya get me?

Ideally I'd like to just have a way for the Pi to recieve requests. I've never done anything transmission based so don't have a clue.

I'm asking here because ideally I'd like to set up a PHP page (Can be hosted on the pi itself on a LAN) with only a box to input the URL and a "send request" button.

Any help at all would be greatly appreciated.

First thing you can do is share the actual original script so we can have a look how it works...


RE: Biggest PHP noob ever - Inori - 12-19-2015

I realise you've requested something PHP based, but if you'd be ok with switching to Node.js, you'll find that this can be accomplished super easily with Socket.IO. (you'd need npm to install the dependencies, but other than that, you're set with just node)

Spoiler: sample
Client (index.html):
Code:
<!DOCTYPE html> <html> <head> <!-- import jquery and socket.io for client use --> <script src='https://cdn.socket.io/socket.io-1.2.0.js'></script> <script src='http://code.jquery.com/jquery-1.11.1.js'></script> <!-- client-side socket.io script --> <script type='text/javascript'> //'require' io() var socket=io(); //on form submit do... $('form').submit(function(){ //emit a request, which will be picked up by the server socket.emit('request',$('#url').val()); //alert client alert('request sent'); $('#url').val(''); return false; }); </script> </head> <body> <!--create div at center --> <div style='width: 300px;height: 300px;position: absolute;left: 50%;top: 50%;margin-left: -150px;margin-top: -150px;'> <!-- create input form --> <form action=''> <input id='url' autocomplete='off'/><button>Send Request</button> </form> </div> </html>

Server (index.js):
Code:
var app=require('express')(); var http=require('http').Server(app); var io=require('socket.io')(http); //feed client the proper page app.get('/',function(req,res){ res.sendFile(__dirname+'/index.html'); }); //listen for connections io.on('connection',function(socket){ //listen for request emission socket.on('request',function(url){ //execute mp3 grabbing code }); }); //tune listener to port 8080 http.listen(8080)



RE: Biggest PHP noob ever - Rick - 12-19-2015

(12-19-2015, 03:31 AM)Nevermore Wrote: I realise you've requested something PHP based, but if you'd be ok with switching to Node.js, you'll find that this can be accomplished super easily with Socket.IO. (you'd need npm to install the dependencies, but other than that, you're set with just node)

Spoiler: sample
Client (index.html):
Code:
<!DOCTYPE html> <html> <head> <!-- import jquery and socket.io for client use --> <script src='https://cdn.socket.io/socket.io-1.2.0.js'></script> <script src='http://code.jquery.com/jquery-1.11.1.js'></script> <!-- client-side socket.io script --> <script type='text/javascript'> //'require' io() var socket=io(); //on form submit do... $('form').submit(function(){ //emit a request, which will be picked up by the server socket.emit('request',$('#url').val()); //alert client alert('request sent'); $('#url').val(''); return false; }); </script> </head> <body> <!--create div at center --> <div style='width: 300px;height: 300px;position: absolute;left: 50%;top: 50%;margin-left: -150px;margin-top: -150px;'> <!-- create input form --> <form action=''> <input id='url' autocomplete='off'/><button>Send Request</button> </form> </div> </html>

Server (index.js):
Code:
var app=require('express')(); var http=require('http').Server(app); var io=require('socket.io')(http); //feed client the proper page app.get('/',function(req,res){ res.sendFile(__dirname+'/index.html'); }); //listen for connections io.on('connection',function(socket){ //listen for request emission socket.on('request',function(url){ //execute mp3 grabbing code }); }); //tune listener to port 8080 http.listen(8080)

Cheers man. Didn't think about using Node, thanks for the help, I'll see if I can get something up and working using that.


RE: Biggest PHP noob ever - ConsoleTVs - 12-23-2015

Just use a POST request:


<?php
if(isset($_POST['url'])) {
$url = $_POST['url'];
echo "The url is: ".$url;
//THE URL IS IN THE $url VARIABLE
//Do some important things here Biggrin
}
?>
<form method='POST'>
<input type='text' placeholder='Video URL' name='url'>
<input type='submit' value='Get URL'>
</form>