Exemple #1
0
 /**
  * Run the process
  *
  * @return array $result
  */
 public function run()
 {
     $urlResult = parent::checkUrl();
     return $this->checkContent($urlResult);
 }
Exemple #2
0
    /**
     * Check if a remote resource exists
     *
     * @see http://www.php.net/manual/en/function.file-exists.php#85246
     * @param string $url
     * @param string $password, see http://www.php.net/manual/en/function.curl-setopt.php
     * @param array(string/string) $acceptablesHttpCodes, see http://fr.wikipedia.org/wiki/Liste_des_codes_HTTP
     * @param string $tmpDir
     * @param bool $fetchBodyContent
     * @return mixed $connectable : CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure
     */
    protected function checkUrl($acceptablesHttpCodes = null, $tmpDir = '/tmp', $fetchBodyContent = true) {
        $result = array();
        if( !filter_var( $this->url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED )
        || !filter_var( $this->url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED )
        )
        {
            throw new ezcUrlException("Can't fetch " . $this->url);
        }
        $result['url'] = $this->url;
        $handle = curl_init($this->url);
        $tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'phpcurl_' . date( 'Y-M-d-H-i-s-u' ) . '.tmp';
        $result['tmpFile'] = $tmpFile;
        $fp = fopen($tmpFile, "w");
        curl_setopt( $handle, CURLOPT_TIMEOUT, '30');
        curl_setopt( $handle, CURLOPT_FAILONERROR, true );  // this works
        curl_setopt( $handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
        curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true ); // will accepts 301
        curl_setopt( $handle, CURLOPT_HEADER, true );
        curl_setopt( $handle, CURLINFO_HEADER_OUT, true );
        curl_setopt( $handle, CURLOPT_NOBODY, !$fetchBodyContent );
        curl_setopt( $handle, CURLOPT_VERBOSE, true );
        curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $handle, CURLOPT_FILE, $fp);

        if( isset( $this->uriOptions->password ) && trim( $this->uriOptions->password !== '') )
        {
            curl_setopt( $handle, CURLOPT_USERPWD, $this->uriOptions->password);
        }
        if( ! $acceptablesHttpCodes )
        {
            $acceptablesHttpCodes = chkBenchmarkUri::acceptablesHttpCodes();
        }
        $result['status'] = curl_exec( $handle );
        if( !$result['status'] )
        {
            throw new ezcUrlException("Can't fetch " . $this->url);
        }
        $result['headerSent'] = curl_getinfo( $handle, CURLINFO_HEADER_OUT );
        $result['result'] = false;
        if( in_array( $result['status'], $acceptablesHttpCodes ) )
        {
            $result['result'] = true;
        }
        $result['code'] = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
        $result['lastUrl'] = curl_getinfo( $handle, CURLINFO_EFFECTIVE_URL );
        $result['contentType'] = curl_getinfo( $handle, CURLINFO_CONTENT_TYPE );
        $result['contentLength'] = curl_getinfo( $handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD );
        $result['fileTime'] = curl_getinfo( $handle, CURLINFO_FILETIME );
        curl_close( $handle );
        fclose($fp);

        $result['content'] = file_get_contents($tmpFile);
        unlink($tmpFile);

        return $result;
    }