PHP function to read and return the page content of a given URL

At times we need to add a page content to a email body. Also some time we need to call a api link to get the data. Here is a function which will help you to read the page content of a given link and return the data in an array. So that you can use the data as per your requirement.

/** FUNCTION TO RETURN PAGE CONTENT OF GIVEN URL****/ 

public function HttpRequest( $url, $method = 'GET', $data = NULL, $additional_headers = NULL, $followRedirects = true )
    {
        $method = strtoupper($method);
        
        /************************/
        /*Parse the given url   */
        /***********************/
        $url_parsed = @parse_url($url);
        if (!@$url_parsed['scheme']) $url_parsed = @parse_url('http://'.$url);
        
        /******************************************************************/
        /*Extract the parseed url values                                                                                  */
        /*It will get the variables $scheme,$host,$port,$user,$pass,$path,$query,$fragment   */
        /****************************************************************/
        extract($url_parsed);
        
        /***********************************************************/
        /*Check if any data is there to post along with the url   */
        /**********************************************************/
        if(!is_array($data))
        {
            $data = NULL;
        }else{
            $ampersand = '';
            $temp = NULL;
            foreach($data as $k => $v)
            {
                $temp .= $ampersand.urlencode($k).'='.urlencode($v);
                $ampersand = '&';
            }
            $data = $temp;
        }
        
        /****************************/
        /*Manipulate the actual url*/
        /***************************/
        if(!@$port) $port = 80;
        if(!@$path) $path = '/';
        if(($method == 'GET') and ($data)) $query = (@$query)?'&'.$data:'?'.$data;
        if(@$query) $path .= '?'.$query;
        $out = "$method $path HTTP/1.0\r\n";
        $out .= "Host: $host\r\n";
        if($method == 'POST')
        {
            $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            $out .= "Content-length: " . @strlen($data) . "\r\n";
        }
        $out .= (@$additional_headers)?$additional_headers:'';
        $out .= "Connection: Close\r\n\r\n";
        if($method == 'POST') $out .= $data."\r\n";
        
        /****************************************************************/
        /*Read the page content and generate the array value for return */
        /****************************************************************/
        if(!$fp = @fsockopen($host, $port, $es, $en, 5)){
           return false;
        }
        fwrite($fp, $out);
        while (!feof($fp)) {
            $s = fgets($fp, 1024);
            
            if ( $s == "\r\n" ) {
                $foundBody = true;
                continue;
            }
            if ( $foundBody ) {
                $body .= $s;
            } else { 
                
                if(($method != 'POST') and ($followRedirects) and (preg_match('/^Location:(.*)/i', $s, $matches) != false) )
                {
                    fclose($fp);
                    return Company::HttpRequest( trim($matches[1]) );
                }
                $header .= $s;
                if(preg_match('@HTTP[/]1[.][01x][\s]{1,}([1-5][01][0-9])[\s].*$@', $s, $matches))
                {
                    $status = trim($matches[1]);
                }
            }
        }
        fclose($fp);
        
        /***************************/
        /* Return the result array */
        /***************************/
return array('head' => trim($header), 'body' => trim($body), 'status' => status);               
    }
     /**END OF FUNCTION****/

    /***************************************/
    /*To call the function using get method*/
    /***************************************/
    $url = 'http://domain/someurl?param1=p1&param2=p2';
    $result = HttpRequest($url);
    echo $result['body'];//This will return the page content
    
    /***************************************/
    /*To call the function using get method*/
    /***************************************/
    $url = 'http://domain/someurl';
    $postvars=array('param1'=>'p1','param2'=>'p2');
    $result = HttpRequest($url,$method='POST',$data=$postvars);
    echo $result['body'];//This will return the page content
150 150 Burnignorance | Where Minds Meet And Sparks Fly!