开发者

How to trigger a URL using php?

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开发者_如何学JAVA.


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); 
?>


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);
}


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.


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.


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


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.


Here is a sample command to call another script from current file as a separate process

exec('/usr/bin/php5 /var/www/vhosts/mysite.com/httpdocs/PROCESS/processScript.php');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜