|
 |
|
 |
03-23-2006, 05:42 PM
|
#1 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
Read web page into an array
I've written a php script to read my current winamp playlist. I need to write javascript to call upon http://josh.avclan.net/playlist.php and read the output into an array of song titles. I don't have a problem doing it in php, but I don't do much javascript, so if anyone can help me out, that'd be fantastic.
I plan to throw this bit of javascript onto my myspace page, otherwise I'd just stick with php
Thanks
|
|
|
03-23-2006, 07:14 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
i have json installed on the server. you can convert a php to a javascript array string by using this:
PHP Code:
<? $array = array('apple', 'orange', 'pear'); echo json_encode($array); ?>
then u can use some ajax stuff like this:
HTML Code:
<script>
// start ajax stuff
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(method, URL) {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(method, URL, true);
xmlHttp.send(null);
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
processResponse(xmlHttp.responseText);
}
}
}
function processResponse(response) {
my_songs = eval(response);
}
// end ajax stuff
// initialize your song array
var my_songs;
// make ajax request
startRequest('GET', 'my_php_script.php');
// now my_songs is set to your php array
</script>
|
|
|
03-23-2006, 08:57 PM
|
#3 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
Thanks Mike, I'm really close to getting this working. The only problem is that my_songs is still "undefined", it isn't getting set to anything, and ajax is greek to me. Arg...
You can see the php version of it at http://josh.avclan.net/ though
|
|
|
03-24-2006, 08:42 AM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
i can't test it myself right now, but try moving the 'var my songs' line to the top.
|
|
|
03-24-2006, 09:25 AM
|
#5 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
 Yeah I thought of that, didn't change anything. Am I correct in pointing the startRequest() function to http://josh.avclan.net/playlist.php ?
The xmlHttp.readyState never changes to 4, it is still 1 when the page loads. I'm trying to look up information on ajax to figure it out though
|
|
|
03-24-2006, 10:10 AM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
change the processResponse to this to make sure it is being triggered and you are getting output:
HTML Code:
function processResponse(response) {
alert(response);
my_songs = eval(response);
}
another thing you may want to try is initialize the song var to an empty array
HTML Code:
var my_songs = new Array();
|
|
|
03-24-2006, 10:19 AM
|
#7 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
processResponse isn't getting triggered because xmlHttp.readyState = 1 and doesn't seem to change. I stripped out the if statements in handleStateChange(), but obviously if the readyState isn't changing from 1, there's no data to eval()
I'm assuming a readyState of 1 means the code isn't able to render the page? Any idea why that would be? It wouldn't have to do with security or permissions would it?
|
|
|
03-24-2006, 10:49 AM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
are you running the page locally? there could be a browser security issue. make sure the files are uploaded to the server. i just tested this code and it works.
test.html
HTML Code:
["First Song","Second Song"]
mypage.html
HTML Code:
<script language="javascript">
var my_songs = new Array();
// start ajax stuff
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(method, URL) {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(method, URL, true);
xmlHttp.send(null);
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
processResponse(xmlHttp.responseText);
}
}
}
function processResponse(response) {
my_songs = eval(response);
for( x in my_songs ) {
alert( my_songs[x] );
}
}
// make ajax request
startRequest('GET', 'test.html');
</script>
|
|
|
03-24-2006, 11:07 AM
|
#9 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
Awesome, it's working now. Not sure what I was doing wrong before, though I might have been trying to access the my_songs array outside of the scope of processResponse. Maybe if I had made it global, I wouldn't have had any issues?
Either way, it works great now. Thanks again Mike
|
|
|
03-24-2006, 11:36 AM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
now that you have a little AJAX structure setup, you can get creative with your song list.
for example, you can use a javascript timeout function to re-call startRequest every 20 seconds or so. Then your songlist could refresh without the browser refreshing.
i'm not sure how much other information winamp provides, but you could probably do some cool stuff.
|
|
|
03-24-2006, 11:41 AM
|
#11 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
I'm interfacting with a pre-existing plugin which turns winamp into a web server. It can accept requests to control playback, change playlists, turn up the volume etc. Basically the plugin allows you to turn winamp into a remote-controlled media player.
If I can figure it out, I'll add play controls to the script, and maybe even configure it to stream audio.
|
|
|
03-24-2006, 11:56 AM
|
#12 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
Dammit. Myspace doesn't support javascript. Go figure. Let's see if I can find a language that myspace will allow.
|
|
|
03-24-2006, 12:47 PM
|
#13 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
Why not make your own blog elsewhere, then all your myspace friends will be jealous.
__________________
Stop intellectual property from infringing on me
|
|
|
03-24-2006, 12:55 PM
|
#14 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
Well, that's actually what I'm working on and the reason why I wrote the php side of it in the first place. My blog is a work in progress and it's placeholder is here: http://josh.avclan.net
I just wanted to get it working on myspace to see if I could do it, and I'd probably be the first person to have something like that on his myspace...but for security reasons they can't allow javascript support on myspace. Bastards.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:43 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|