Current location: Hot Scripts Forums » Programming Languages » PHP » Simple? Get result from http: in PHP


Simple? Get result from http: in PHP

Reply
  #1 (permalink)  
Old 07-10-03, 11:04 AM
wilson@cpuworks.com wilson@cpuworks.com is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Question Simple? Get result from http: in PHP

I want to execute a program on another server,
and access the results in my PHP program.
This seems like it should be simple, but I'm
experiencing brain lock.
This works, outside of PHP:
I enter this as my URL in the browser:
http://server.com/cgi-bin/qdir?fm=X&uid=myuid
and receive back a chunk of XML data.
I'd like to execute that EXACT same URL in my
PHP program, with the proper user id substituted
for "myuid", and get that chunk of data into a string
where I can parse it to my heart's content.
What I tried:
$output = exec('http://server.com/cgi-bin/qdir?fm=X&uid=myuid');
echo "<pre>$output</pre>";

or "system(---)" or backticks with the HTTP command, etc.
All return $output=(blank).
The fact that it's an HTTP command has me confused;
it's not quite the same as executing a command on the
local machine (exec('/cgi-bin/qdir[etc]'))....
What am I missing? I'm sure it's obvious and simple,
but I just can't picture it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-10-03, 11:23 AM
BdSBB BdSBB is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Georgetown, TX
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
You could try fopen() function.

PHP Code:

$fh fopen ('http://server.com/cgi-bin/qdir?fm=X&uid=myuid''r');

fpassthru($fh);
fclose($fh); 
__________________
-- BdSBB
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-10-03, 12:37 PM
wilson@cpuworks.com wilson@cpuworks.com is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Bad news: Oddly enough, I get an error on the fclose with this
approach: Warning: fclose(): 1 is not a valid File-Handle
resource....
Good news: the fpassthru() does work, sending the output
to the screen.

But... I want to put the results in a string, not
just pass them directly back. So I tried to use, instead
of the fpassthru($fh):
$output = readfile($fn);

But that also gives an error, which makes sense when I read
the manual (!!!), which says that for HTTP fopens,
"an HTTP 1.0 connection is opened to the specified server
and a file pointer is returned to the beginning of the text
of the response." Fine, but then what? This means
$fh = 1 or something, and how do I actually get the text?
The readfile($fh) doesn't work, which makes sense if the
$fh is a file pointer; I get an error on the readfile saying
that this is not a valid resource. Anybody got a short
example of getting this to work? Should be easy, but I'm
still baffled.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-10-03, 12:58 PM
wilson@cpuworks.com wilson@cpuworks.com is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Never mind, I got it.
$fh = fopen("http://myserver.com/cgi-bin/qdir?a=b&uid=abc", "r");
$output = '';
while (! feof($fh)) {
$output .= fread($fh, 1048576);
}
fclose($fh);

// at this point, $output contains the output, as desired,
// ready for parsing, etc! Cool....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
PHP Dudes - Hi All stuart New Members & Introductions 4 05-03-04 02:22 PM
Custom PHP Scripts BdSBB Job Offers & Assistance 3 12-07-03 10:49 AM
looking for a simple forum php code westwood Script Requests 5 10-07-03 03:26 AM
PHP Triad/Upload form eddyvlad PHP 6 10-07-03 12:17 AM


All times are GMT -5. The time now is 12:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.