Configure curl function to support boot and info header in php

Hi, I have time to create my own curl function:

Inputs are everything (url for files, images, web pages, etc.)

I have several scenarios:

scenario1

$url = 'an broken link or file';
$type =  null; //not important
callCurl($url,$type,10,2); 

      

I am excluding the return false function due to if($curl = curl_init($url))

but not working. (I am testing it with a broken file url)

scenario2

$url = 'an active url file';
$type =  'info'
callCurl($url,$type,10,2); 

      

Except I only return the file size, but I download the file first and then give me the size!

scenario3

$url = 'an active url webpage';
$type =  'content'
callCurl($url,$type,10,2);

      

I except the returned loaded web page, it works. no problems!

scenario4

$url = 'an active url of file or images';
$type =  'content'
callCurl($url,$type,10,2);

      

How do I upload a file or images? is this the same as script3?

This is the function:

//$type: info | content 
// info: for example size of file or webpages without downloading 
// content: dpwnloaded content (file/webpage/image/etc...) 
function callCurl($url,$type,$timeout,$ctimeout)
{
    if($curl = curl_init($url))
    {
        curl_setopt( $curl, CURLOPT_NOBODY, true );
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_REFERER, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );//
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,$ctimeout); 
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); //timeout in seconds
        curl_setopt($curl, CURLOPT_SSLVERSION,3);    
        $str = curl_exec($curl);
        if(curl_errno($curl))
        {
            echo curl_error($curl);
            curl_close($curl);
            return false;
        }
        else
        {
            if($type == 'info')
            {
                $info['mimeType'] = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
                $info['size'] = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
                $info['path'] = parse_url($url, PHP_URL_PATH);
                $info['filename'] = substr($info['path'], strrpos($info['path'], '/') + 1);
                curl_close($curl);
                return $info;
            }
            elseif($type == 'content')
            {
                return $str;
            }

        }
    }
    echo "This URL IS NOT ACTIVE IT IS NOT 200";
    return false;
}

      

how can i change it to support these senarii?

+3


source to share


1 answer


For scenario1: try changing if(curl_errno($curl))

to if(0 != curl_errno($curl))

.

curl_errno returns the error number, or 0 (zero) if no error occurred.



For scenario2: you need to add curl_setopt($ch, CURLOPT_HEADER, true)

. This does not download the file, it only fetches the header. You find Content-Length

when parsing the returned data.

0


source







All Articles