MrE
MrE

Reputation: 1154

Any alternative to curl_exec()?

I am trying to make a sort of license checking script, but as not all hosts allow curl_exec, I would like to know, if there is any alternative way of making a call-back?

This is how I do it with curl:

curl_setopt($ch, CURLOPT_URL, $url."/my_script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $info);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);

Upvotes: 2

Views: 4563

Answers (2)

ItalyPaleAle
ItalyPaleAle

Reputation: 7316

You can simply use file_get_contents() with URLs:

file_get_contents('http://www.google.com');

This may be disabled, though, with just a simple INI option allow_url_fopen.

Alternatively, you may use fsockopen(), which should be available on most systems.

With fsockopen() you can open a socket to a HTTP server, then communicate using standard fwrite() and fread(). The downside is that you must write HTTP request headers by yourself, and you must also parse HTTP response headers too. If you look at fsockopen() on PHP's manual, you can see plenty of examples: https://www.php.net/fsockopen

My suggestion is to use cURL as primary option, file_get_contents() as secondary (if ini_get('allow_url_fopen') returns a positive result) and implement solutions like fsockopen() as fallback.

Upvotes: 3

phihag
phihag

Reputation: 288090

You could switch curl_multi_exec, but that's probably disabled as well.

Alternatively, use the HTTP stream wrapper. To configure a POST request, you'll need to set up your own HTTP context.

Upvotes: 2

Related Questions