tags:

views:

275

answers:

6

I want to trigger a URL without user interaction. I used

file_get_contents($myurl);

But in my webhost file_get_contents() is disabled. how to do that. I dont want to read the contents of the URL. I just want to trigger the URL. Also I can not use header() function, because, Im going to trigger the url in a loop, so header will not be helpful.

MY PHP INFO IS sivakasian.cwahi.net/info.php

plz help

+1  A: 

Use cURL:

<?php
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com'); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec(); 
curl_close($ch); 
?>
Dominic Rodger
For this I have to include or install anything??
Rajasekar
Yes, it's PHP extention and it must be installed and enabled
Ivan Nevostruev
You'll need to have cURL installed (see http://us.php.net/manual/en/curl.requirements.php).
Dominic Rodger
Judging by his PHP Info, he doesnt have CURL installed.
Gordon
See my answer. Internally it should be exactly the same (retrieve the headers, but not the body). The good thing is, you don't need to have any library installed as is the case with Curl, since get_headers is built-in.
Exception e
Ah, I need to take that back. `allow_url_fopen` is Off, no php solution will work. @Gordon's workaround is your only option.
Exception e
+2  A: 

You could try some lower level socket calls to make a simple HTTP request with fsockopen and related functions. Here's a sample from the manual

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
   $out = "GET / HTTP/1.1\r\n";
   $out .= "Host: www.example.com\r\n";
   $out .= "Connection: Close\r\n\r\n";

   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}
Paul Dixon
fsockopen() also disabled by my webhost
Rajasekar
Well, I would take that as a sign that maybe your host just doesn't want you do this. Find another host!
Paul Dixon
Any free php hosting site u know??
Rajasekar
You're likely not going to get what you want for free. Quality service costs money. Pay for a decent webhost.
Wade Williams
+2  A: 

The other answers are the right way to go to actually "trigger" the other URLs. But it is worth mentioning that if you're trying to request those URLs "without user interaction" you should consider running a Cron Job to actually make it happen, rather than relying on someone to access a specific page on your website.

philfreo
I want to trigger the URL based on Condition, Not on time interval
Rajasekar
+1  A: 

Well, if you cannot use Curl or fsockopen or exec wget or any of the other suggestions to make it work from PHP, it is a lost cause. Almost. If you dont have to have the URL called during script execution, you could let the client call the url by adding an WebBug element to the html you send back to browser (assuming this is what you do), e.g.

<img src="http://www.example.com/trigger.php?arg1=foo" width="1" height="1"/>

If you need the response from the script, you could use an iframe, though you should check if that element is part of the Doctype you are using. You could also try via JavaScript, but have to find a way to bypass the Same Origin Policy most browsers enforce.

If that's still not solving your problem, try another free hosting service or consider switching to paid hosting. You can get a decent setup for below ten bucks nowadays.

Gordon
Since `allow_url_fopen` is Off, this is the last resort indeed. Good catch.
Exception e
A: 

If none of the other options work for you, you might be able to do it using the shell. Using wget:

shell_exec('wget http://example.com/');

If wget isn't installed, you might be able to do it with lynx.

http://php.net/manual/en/function.shell-exec.php

Paul McMillan
A: 

update: ah, I see allow_url_fopen is off. Yeah, then even get_headers won't work. Then the solution from @Gordon is the way too go. A bit nasty, but there is no other option.
I would recommend to avoid such hosters like the plague, this is too limiting.


In your last question I answered get_headers($url). Thats all. This is definitively the way I would do it. It returns the headers, so that you are able to see if your request was handled properly.

Curl is also an option but I doubt if this is installed on your shared hosting, which seems to be restrictive.

Exception e