<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Geek is a Lift-Style. &#187; php curl</title>
	<atom:link href="http://www.taiwangeek.com/category/php/php-curl/feed" rel="self" type="application/rss+xml" />
	<link>http://www.taiwangeek.com</link>
	<description>Time has a wonderful way of showing us what really matters.</description>
	<lastBuildDate>Mon, 23 May 2011 07:18:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>use curl to got Content-Disposition filename</title>
		<link>http://www.taiwangeek.com/2011-01/use-curl-to-got-content-disposition-filename.html</link>
		<comments>http://www.taiwangeek.com/2011-01/use-curl-to-got-content-disposition-filename.html#comments</comments>
		<pubDate>Mon, 10 Jan 2011 03:42:12 +0000</pubDate>
		<dc:creator>tung</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php curl]]></category>
		<category><![CDATA[原創]]></category>

		<guid isPermaLink="false">http://www.taiwangeek.com/?p=5655</guid>
		<description><![CDATA[curl CURLOPT_BINARYTRANSFER can't not got Content-Disposition filename for u]]></description>
			<content:encoded><![CDATA[<pre class="brush:php">/*
* usage:
=================================================
$fn = CurlTool::downloadFile('http://aaabb.com/download.php?id=zzzaaa','./');
echo "Grab file path =",$fn,"\n";
var_dump(CurlTool::$attach_info);
*/
class CurlTool {
    public static $userAgents = array(
        'FireFox3' =&gt; 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
        'GoogleBot' =&gt; 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        'IE7' =&gt; 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
        'Netscape' =&gt; 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
        'Opera' =&gt; 'Opera/9.25 (Windows NT 6.0; U; en)'
        );
    public static $options = array(
        CURLOPT_USERAGENT =&gt; 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
        CURLOPT_AUTOREFERER =&gt; true,
        CURLOPT_COOKIEFILE =&gt; '',
        CURLOPT_FOLLOWLOCATION =&gt; true
        );
    public static $header = array();
    public static $attach_info = array();

    private static $proxyServers = array();
    private static $proxyCount = 0;
    private static $currentProxyIndex = 0;

    public static function addProxyServer($url) {
        self::$proxyServers[] = $url;
        ++self::$proxyCount;
    }

    public static function fetchContent($url, $verbose = false,$url_reffer=false) {
        if (($curl = curl_init($url)) == false) {
            throw new Exception("curl_init error for url $url.");
        }

        if (self::$proxyCount &gt; 0) {
            $proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
            curl_setopt($curl, CURLOPT_PROXY, $proxy);
            if ($verbose === true) {
                echo "Reading $url [Proxy: $proxy] ... ";
            }
        } else if ($verbose === true) {
            echo "Reading $url ... ";
        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt_array($curl, self::$options);
        if (is_string($url_reffer)) {
            curl_setopt($curl,CURLOPT_REFERER,$url_reffer);
        }
        $content = curl_exec($curl);
        if ($content === false) {
            throw new Exception("curl_exec error for url $url.");
        }
       	self::$header = curl_getinfo($curl);
        curl_close($curl);
        if ($verbose === true) {
            echo "Done.\n";
        }
        return $content;
    }

    public static function downloadFile($url, $fileName, $verbose = false) {
        if (($curl = curl_init($url)) == false) {
            throw new Exception("curl_init error for url $url.");
        }

        if (self::$proxyCount &gt; 0) {
            $proxy = self::$proxyServers[self::$currentProxyIndex++ % self::$proxyCount];
            curl_setopt($curl, CURLOPT_PROXY, $proxy);
            if ($verbose === true) {
                echo "Downloading $url [Proxy: $proxy] ... ";
            }
        } else if ($verbose === true) {
            echo "Downloading $url ... ";
        }

        curl_setopt_array($curl, self::$options);

        if (substr($fileName, -1) == '/') {
            $targetDir = $fileName;
            $fileName = tempnam(sys_get_temp_dir(), 'c_');
        }
        if (($fp = fopen($fileName, "wb")) === false) {
            throw new Exception("fopen error for filename $fileName");
        }
        curl_setopt($curl, CURLOPT_FILE, $fp);
		curl_setopt($curl,CURLOPT_HEADERFUNCTION,'self::get_att');
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
        if (curl_exec($curl) === false) {
            fclose($fp);
            unlink($fileName);
            throw new Exception("curl_exec error for url $url.");
        } elseif (isset($targetDir)) {
            $eurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
            preg_match('#^.*/(.+)$#', $eurl, $match);
            fclose($fp);
            rename($fileName, "$targetDir{$match[1]}");
            $fileName = "$targetDir{$match[1]}";
        } else {
            fclose($fp);
        }
		self::$header = curl_getinfo($curl);
        curl_close($curl);
        if ($verbose === true) {
            echo "Done.\n";
        }
        return $fileName;
    }

	public static function get_att($ch, $header){
		if (preg_match("/Content-Length: (\d*)/i",$header,$matches)) {
			self::$attach_info['size'] = $matches[1];
		} elseif (preg_match('/Content-Disposition:.*?filename="(.*)"/i',$header,$matches)) {
			self::$attach_info['filename'] = $matches[1];
		} elseif (preg_match("/Content-Type: ([^; ]*)/i",$header,$matches)) {
			self::$attach_info['type'] = $matches[1];
		}
		//echo "$header";
		return strlen($header);
	}
    // activates POST and set post data
    public static function addPostData($data) {
        self::$options[CURLOPT_POST] = true;
        self::$options[CURLOPT_POSTFIELDS] = $data;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.taiwangeek.com/2011-01/use-curl-to-got-content-disposition-filename.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (request URI doesn't have a trailing slash)

Served from: www.taiwangeek.com @ 2012-05-19 06:02:48 -->
