access a php script on one server from another?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robophobic
Forum Newbie
Posts: 3
Joined: Thu Aug 26, 2004 8:14 am

access a php script on one server from another?

Post by robophobic »

Here's what I'm needing to do:
I have a script on one server that lists the contents of a folder (images). The script works just fine locally and when called by a browser. I'm setting up a "master" server, on a different domain, and it needs access to that script.
So on the master server, the script needs to run and display the images from the remote server, by accessing the php file ON the remote server. I've tried an INCLUDE but that did nothing. I've also tried to "read" the remote file as if it were a text file, but again that did nothing.
Is there a way to do this?
Even if it's a kludgy work-around, I'd be happy!

Thanks in advance!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I believe the only thing you could do is read the output of the PHP file. If the PHP file outputs a page of <img> tags, you could read that in, & parse out the filenames.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
robophobic
Forum Newbie
Posts: 3
Joined: Thu Aug 26, 2004 8:14 am

Post by robophobic »

Thanks for your reply. How exactly would I do the reading? Opening it as if it were a text file, or including it? Or is there some other method that would work?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Your system has to be set up for it, but you can use URL wrappers for some file opening functions. My favorite is file(). If you can call:

Code: Select all

$file_contents = file('http://other.server.com/listing.php');
That'd be the way to do it. file() generates an array with each file line being an array entry. You could loop through that & pull out the image names with some fancy regex ;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You might run into a permissions issue with though. I think I tried this once or twice and got spanked by the server for doing so.

You may want to look at cURL as an alternative. But I could also be way off here.
robophobic
Forum Newbie
Posts: 3
Joined: Thu Aug 26, 2004 8:14 am

Post by robophobic »

Thanks so much for your quick replies, guys!

It's looking like there's something standing in the way of this working, and as i don't have access to the servers themselves to change the PHP settings, it looks like I'm going to have to take another path.
So here's what i'm thinking:
The main script, on the main site, checks a DB for a username and password, and the DB then returns the URL of that person's site. I was then going to have the script give them access to an admin page where they could edit text that is on their site. Since it's not looking like this is going to work with my servers' configs, I'm thinking now that I should instead have the main script check the DB, and then redirect the browser window to an admin script residing on the other server.

Only...

I HAVE NO IDEA HOW TO MAKE PHP REDIRECT.

dangit.
A search for "redirect" just brings up actual redirecting, not auto-loading another URL based on the results of an IF().

Maybe I'm searching for the wrong term?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"header location" may give more useful results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Or the PHP manual on the header() function.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

Sounds like something that could be achieved using SOAP. SOAP is basically a way for your php program to call functions from a remote script. It would require refactoring your code, and you would need to load the soap extension on both servers. This would be the most ideal solution to your problem based on what you're trying to do.

Another possibility, as someone had mentioned, would be to use cURL. This is also an extension, but would require no change to the original script, just your script trying to access the original. It basically turns your script into an HTTP client, grabbing the output (html) of whatever URL you are trying to access.
Post Reply