Search code examples
phpcurlfgetsfputs

fputs GET to curl


I have this script which requires allow_url_fopen = On which is not a good idea and I want to change curl. I'll appreciate if any one can help me out with how to do the fputs GET.

$SH = fsockopen($IP, $Port, $errno, $errstr, 30);
echo $errstr;    
#$ch = curl_init();
#curl_setopt($ch, CURLOPT_TIMEOUT, 30);
#curl_setopt($ch, CURLOPT_URL, $IP.":".$Port);
#curl_setopt ($ch, CURLOPT_HEADER, 0);
    if ($SH){ 
        fputs($SH,"GET /7.html HTTP/1.0\r\nUser-Agent: S_Status (Mozilla Compatible)\r\n\r\n");
        $content = "";
        while(!feof($SH)) { 
            $content .= fgets($SH, 1000);
            #content .= curl_setopt($ch, CURLOPT_FILE, $SH);
        }
        fclose($SH);
        #curl_close($ch);
}

Solution

  • If I understand your question correctly, you're just looking for how to send the HTTP request (that's what your fputs($SH, "GET ...") is doing, that is done using curl_exec(). Seems like this should work.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_PORT, $Port);
    curl_setopt($ch, CURLOPT_URL, $IP . '/7.html');
    curl_setopt($ch, CURLOPT_USERAGENT, 'S_Status (Mozilla Compatible)'); 
    //tell curl to return the output, not display it
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    //execute the request
    $content = curl_exec($ch);