Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php libcurl alternative

Tags:

php

curl

libcurl

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

like image 780
Phil Avatar asked Jun 05 '26 21:06

Phil


1 Answers

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.

like image 120
Pascal MARTIN Avatar answered Jun 07 '26 11:06

Pascal MARTIN