Home > php教程 > php手册 > PHP使用流类Stream读cUrl下载内容实例

PHP使用流类Stream读cUrl下载内容实例

WBOY
Release: 2016-06-02 09:13:40
Original
1352 people have browsed it

在C++,C#,PHP等编程语言中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,现在我们来讲讲PHP使用Stream读取cUrl下载结果。

使用 stream 的好处就是不会因为数据大小而占用大量的内存,现在我们来分享代码实例。

<?php
$url = &#39;http://www.google.com.tw&#39; ;    
/* 建立接收的Temp File Stream */    
$tmpfile = tmpfile ();    
$curl = curl_init ();    
/* 指定下载的的URL */    
curl_setopt ( $curl , CURLOPT_URL, $url );     
/* 指定存放的File Stream */    
curl_setopt ( $curl , CURLOPT_FILE, $tmpfile );    
/* 执行并取得状态*/    
$status = curl_exec ( $curl );    
curl_close ( $curl );    
if (! $status ){     
   fclose ( $tmpfile );     
   exit ( &#39;error&#39; );     
}    
/* Temp File Stream 指标归零*/    
fseek ( $tmpfile , 0);    
/*一次读取一行*/    
while (( $line = fgets ( $tmpfile )) !== false ) {    
   var_dump ( $line );    
}    
/* 关闭Stream */    
fclose ( $tmpfile );
Copy after login


文章地址:

转载随意^^请带上本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template