Forum Moderators: coopster

Message Too Old, No Replies

PHP function similar to Perl get()?

         

rjbearcan

12:26 am on Nov 16, 2005 (gmt 0)

10+ Year Member



In Perl, the get() function tries to get info from a URL...is there a similar function in PHP?

Thanks a lot.

a1call

12:37 am on Nov 16, 2005 (gmt 0)

10+ Year Member



H rjbearcan,
This might help:
[ca.php.net...]

$_GET['any-text']

would contain
"the-text"

if URL is:

http://example.com/?any-text=the-text

JeffSela

2:14 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



Or do you mean the LWP 'get' function, for issuing an HTTP GET request to download a page? In Perl I've used this sort of thing:


use LWP::Simple;
my $result = get("http://example.com/");
if (defined $result) {
# It worked, the content of the page is in $result.
print $result;
}
else {
print "Error with request.\n";
}

I don't know of a way that's as simple as that in PHP, but you can use the Curl library to do that sort of thing. The php.net docs for that are here, with some examples:

[php.net ]

Although by default it just dumps the downloaded data onto your output. You can redirect it to a file, and there must be a way to download it into a variable. Haven't tried that yet though.

directrix

2:35 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



In curl, to download into a variable, set the returntransfer option prior to the exec. For example:

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, 'http://www.example.com/');
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache', 'Host: www.example.com', 'Connection: close'));
curl_setopt ($ch, CURLOPT_USERAGENT, 'Hungry spider');
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);

$lines = curl_exec ($ch);

curl_close ($ch);

rjbearcan

4:27 pm on Nov 22, 2005 (gmt 0)

10+ Year Member



Sorry for being late but thanks for the help.