I know that this is a simple question for PHP guys but I don't know the language and just need to do a simple "get" from another web page when my page is hit. i.e. signal the other page that this page has been hit.
EDIT: curl is not available to me.
If curl wrappers are on (they are per default), you can use:
file_get_contents('http://www.example.org');
Note that this happens synchronous, so before the request has completed, your page won't either. It would be better to log access to a logfile (or database) and export the data occasionally. Alternatively, you could do the request after your page has completed, and output has been sent to the client.
Beware file_get_contents() and fopen():
If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.
There's numerous ways... the simplest is file_get_contents('http://...');
Other functions like fopen() also support http: streams.
If you need more control, I'd suggest looking at curl (see curl_init)
There are a number of ways to send a GET request with PHP. As mentioned above, you can use file_get_contents, fopen or cURL.
You can also use the HTTP extension, the fsockopen() function or streams via fopen.
I'd advise checking out what WordPress has done, as it handles almost all possibilities.
You'll probably want to use cURL
Related
I'm trying to download file from remote url with fopen function.
Problem it's function return false from one website that i need. From other domains functions works fine.
How could it be? Maybe have some options in php? Or that website can protect file from some access(but from browser file available)?
There are a number of checks the server side can do to prevent "miss usage" of their service. One example is a check of the "HTTP Referer Header" which indicates that your request is done by a browser navigating from a link to the object.
You can simulate all that if you want to, but for that you have to find out exactly what the difference is between your request and one the browser successfully makes. Two things to do for that:
find out the exact error message you receive back. Easiest for that is to use php's cURL extension instead of file_open() for your request, it allows you to dump everything you get back. There might be valuable information like a reason in the reply.
monitor both requests by means of a network sniffer, for example tcpdump or wireshark. The comparison of both requests allows to tell the exact difference. That again is the information you need to precisely rebuilt the browsers request in your script.
On some shared hosting or some VPS fopen not work or are disabled inside PHP. Try to use CURL to get contnt. If that not work, the last solution (only if you send some informations via GET but not to recive data) is to use <img> tag and inside "src" to send request. That work ONLY if you send informations, but if you need to recive something, you need to use or AJAX or cURL.
How does PHP's function xmlrpc_encode_request() work?
Does it need the value of allow_url_fopen set to true?
Well, you access external URLs with this function, so does it need this configuration? I can't test it myself at the moment, unfortunately.
Thanks in advance!
No, it does not. This function is just for marshalling the requested function name and its parameters. It builds an XML string. Therefore it does not need to retrieve external URLs itself.
So this function in itself is not particular useful. Later on you will need to send the XML-encoded RPC request over an HTTP stream. And in this step 2, you need either allow_url_fopen, the curl extension, or the pecl http functions. Most XML-RPC libraries however provide a means to transmit the RPC request in HTTP via PHPs native socket functions.
So in essence, allow_url_fopen is not required.
It seems like I can't use shell_exec or proc_open on my shared server.
The message I get when I try to use it is:
Warning: shell_exec() has been disabled for security reasons in /home/georgee/public_html/admin/email.php on line 4
Are there any alternatives to these functions?
I assume you want to use this for async processing, for instance sending eMails in a separate process (hence the error in email.php). If so, please check if cURL is enabled. You can trigger your scripts through an HTTP request without waiting for the response.
Further reading:
Asynchronous/parallel HTTP requests using PHP multi_curl
How to use curl_multi() without blocking
How do I make an asynchronous GET request in PHP?
If cURL is not available, try to use with non-blocking streams:
http://de.php.net/manual/en/function.stream-set-blocking.php
http://thethoughtlab.blogspot.com/2007/04/non-blocking-io-with-php-mio.html
For added security, consider using a self-defined API key, so the eMail sending URL cannot be triggered without permission. Pass this in as a GET or POST. You could also validate if the request came from the same server.
The only alternative is to not use shared hosting. They don't want you doing this for a reason.
Try using backtick operator
This answers seems to suggest these is not much you can do.
Just a follow up for people who need to use shared hosting. While I no longer recommend HostGator due to their sub-par support (resulting from their purchase by EIG), they do allow popen.
I have an XML file I can get via a URL. I know I can get the file using fopen, but sometimes I've seen scripts use curl. Is there an advantage to using curl over fopen to get XML files?
allow_url_fopen, which is required if you want to open a remote file with fopen, can be disabled ; so, there are situations in which fopen('http://...') is not possible.
Note : in this answer, I say "fopen", but it's exactly the same with all PHP functions that can access remote files : fopen, file_get_contents, simplexml_load_file, ...
On the other hand, curl is an extension, and is not always enabled either.
One nice with curl is that it's pretty easy to configure, and there are a lot of existing options (see curl_setopt)
To configure the way fopen accesses remote files, it's a bit trickier -- you'll generally have to work with streams (see here, for example) ; and, generally speaking, there are more people knowing curl than there are developpers mastering streams.
Safest way -- especially if your application will be deployed to servers on which you are not administrator, and cannot re-configure :
Try one solution
And, if it doesn't work, try the other one
Well, if you are going to use SimpleXML to load the file you can use
simplexml_load_file($filename);
However, some servers will restrict loading urls from this function. In this case you would be restricted to cURL.
fopen is simpler to use, and I think not all server setups support curl out of the box. If fopen works fine for you it's probably your best choice.
I am looking for some advice on the best way to retrieve information from a web page (my own site) and then search through that information to find a certain text.
Keeping in mind that some of the servers that run PHP/Joomla do not have cURL enabled, I don't want to cause any unexpected errors.
I've looked at both fopen() and file_get_contents() and both seem to have conflicting views of each other in terms of which will work with/without cURL.
They will both work without curl, but you need to have allow_url_fopen enabled. You can check that by executing phpinfo(). There are security implications however, see this:
Should I allow 'allow_url_fopen' in PHP?
So to grab pages, you can use fopen(), file_get_contents() or some other independent HTTP client implemented in PHP such as HttpClient
that can function without those.