tags:

views:

270

answers:

4

Are there any alternatives to using curl on hosts that have curl disabled?

+1  A: 

http://www.phpclasses.org is full of these "alternatives", you can try this one: http://www.phpclasses.org/browse/package/3588.html

Havenard
that's great answer, thanks!
Phil
+5  A: 

To fetch content via HTTP, first, you can try with file_get_contents ; your host might not have disabled the http:// stream :

$str = file_get_contents('http://www.google.fr');

Bit this might be disabled (see allow_url_fopen) ; and sometimes is...


If it's disabled, you can try using fsockopen ; the example given in the manual says this (quoting) :

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

Considering it's quite low-level, though (you are working diretly with a socket, and HTTP Protocol is not that simple), using a library that uses it will make life easier for you.

For instance, you can take a look at snoopy ; here is an example.

Pascal MARTIN
what about POST?
Phil
file_get_contents will not be OK for POST ; with Snoopy, looking at the readme file, the submit method might do.
Pascal MARTIN
Excellent post. I would suggest that using HTTP/1.0 is easier for someone unfamiliar with HTTP though.
Rushyo
+1  A: 

You can write a plain curl server script with PHP and place it on curl-enabled hosting, and when you need curl - you make client calls to it when needed from curl-less machine, and it will return data you need. Could be a strange solution, but was helpful once.

PHP thinker
+1  A: 

All the answers in this thread present valid workarounds, but there is one thing you should keep in mind. You host has, for whatever reason, deemed that making HTTP requests from your web server via PHP code is a "bad thing", and have therefore disabled (or not enabled) the curl extension. There's a really good chance if you find a workaround and they notice it that they'll block your request some other way. Unless there's political reasons forcing you into using this particular host, seriously consider moving your app/page elsewhere if it need to make http requests.

Alan Storm