Class SaeFetchurl

Description

SAE数据抓取class

在SAE下不能直接使用file_get_contents或者curl抓取外部数据.
而SaeFetchurl允许你抓取外部数据.支持的协议为http/https.

默认超时时间:

  • 连接超时: 5秒
  • 发送数据超时: 30秒
  • 接收数据超时: 40秒
抓取页面
  1.  $f new SaeFetchurl();
  2.  $content $f->fetch('http://sina.cn');

发起POST请求

  1.  $f new SaeFetchurl();
  2.  $f->setMethod('post');
  3.  $f->setPostDataarray('name'=> 'easychen' 'email' => 'easychen@gmail.com' 'file' => '文件的二进制内容') );
  4.  $ret $f->fetch('http://photo.sinaapp.com/save.php');
  5.  
  6.  //抓取失败时输出错误码和错误信息
  7.  if ($ret === false)
  8.          var_dump($f->errno()$f->errmsg());

错误码参考:

  • errno: 0 成功
  • errno: 600 fetchurl 服务内部错误
  • errno: 601 accesskey 不存在
  • errno: 602 认证错误,可能是secretkey错误
  • errno: 603 超出fetchurl的使用配额
  • errno: 604 REST 协议错误,相关的header不存在或其它错误,建议使用SAE提供的fetch_url函数
  • errno: 605 请求的URI格式不合法
  • errno: 606 请求的URI,服务器不可达。

  • author: zhiyong
  • version: $Id$

Located in /saefetchurl.class.php (line 55)

SaeObject
   |
   --SaeFetchurl
Method Summary
SaeFetchurl __construct ([ $akey = NULL], [ $skey = NULL])
string errmsg ()
int errno ()
mixed fetch (string $url, [array $opt = NULL])
int httpCode ()
array responseCookies ([bool $all = true])
array responseHeaders ([bool $parse = true])
void setAllowRedirect ([bool $allow = true])
void setAllowTrunc (bool $allow)
void setAuth (string $akey, string $skey)
void setConnectTimeout (int $ms)
void setCookie (string $name, string $value)
bool setHeader (string $name, string $value)
void setHttpAuth (string $username, string $password)
void setMethod (string $method)
bool setPostData (array $post_data, [bool $multipart = false])
void setReadTimeout (int $ms)
void setSendTimeout (int $ms)
Methods
Constructor __construct (line 57)
SaeFetchurl __construct ([ $akey = NULL], [ $skey = NULL])
  • $akey
  • $skey
errmsg (line 410)

返回错误信息

  • author: zhiyong
  • access: public
string errmsg ()
errno (line 392)

返回错误码

  • author: zhiyong
  • access: public
int errno ()
fetch (line 300)

发起请求

  • return: 成功时读取到的内容,否则返回false
  • author: zhiyong
  • access: public
mixed fetch (string $url, [array $opt = NULL])
  • string $url
  • array $opt: 请求参数,格式:array('key1'=>'value1', 'key2'=>'value2', ... )。详细参数设置请参考SaeFetchurl::setOpt()
httpCode (line 347)

返回HTTP状态码

  • author: Elmer Zhang
  • access: public
int httpCode ()
responseCookies (line 359)

返回头里边的cookie信息

  • author: zhiyong
  • access: public
array responseCookies ([bool $all = true])
  • bool $all: 是否返回完整Cookies信息。为true时,返回Cookie的name,value,path,max-age,为false时,只返回Cookies的name, value
responseHeaders (line 316)

返回数据的header信息

  • author: zhiyong
  • access: public
array responseHeaders ([bool $parse = true])
  • bool $parse: 是否解析header,默认为true。
setAllowRedirect (line 275)

当请求页面是转向页时,是否允许跳转,SAE最大支持5次跳转(默认不跳转)

  • author: zhiyong
  • access: public
void setAllowRedirect ([bool $allow = true])
  • bool $allow: 是否允许跳转。true:允许,false:禁止,默认为true
setAllowTrunc (line 231)

是否允许截断,默认为不允许

如果设置为true,当发送数据超过允许大小时,自动截取符合大小的部分;
如果设置为false,当发送数据超过允许大小时,直接返回false;

  • author: zhiyong
  • access: public
void setAllowTrunc (bool $allow)
  • bool $allow
setAuth (line 82)

设置acccesskey和secretkey

使用当前的应用的key时,不需要调用此方法

  • author: zhiyong
  • access: public
void setAuth (string $akey, string $skey)
  • string $akey
  • string $skey
setConnectTimeout (line 242)

设置连接超时时间,此时间必须小于SAE系统设置的时间,否则以SAE系统设置为准(默认为5秒)

  • author: zhiyong
  • access: public
void setConnectTimeout (int $ms)
  • int $ms: 毫秒
setCookie (line 215)

在发起的请求中,添加cookie数据,此函数可多次调用,添加多个cookie

  • author: zhiyong
  • access: public
void setCookie (string $name, string $value)
  • string $name
  • string $value
setHeader (line 173)

在发起的请求中,添加请求头

不可以使用此方法设定的头:

  • Content-Length
  • Host
  • Referer
  • Vary
  • Via
  • X-Forwarded-For
  • FetchUrl
  • AccessKey
  • TimeStamp
  • Signature
  • AllowTruncated //可使用setAllowTrunc方法来进行设定
  • ConnectTimeout //可使用setConnectTimeout方法来进行设定
  • SendTimeout //可使用setSendTimeout方法来进行设定
  • ReadTimeout //可使用setReadTimeout方法来进行设定

  • author: zhiyong
  • access: public
bool setHeader (string $name, string $value)
  • string $name
  • string $value
setHttpAuth (line 287)

设置HTTP认证用户名密码

  • author: zhiyong
  • access: public
void setHttpAuth (string $username, string $password)
  • string $username: HTTP认证用户名
  • string $password: HTTP认证密码
setMethod (line 111)

设置请求的方法(POST/GET/PUT... )

  • author: zhiyong
  • access: public
void setMethod (string $method)
  • string $method
setPostData (line 124)

设置POST方法的数据

  • author: zhiyong
  • access: public
bool setPostData (array $post_data, [bool $multipart = false])
  • array $post_data: , key为变量名称,value为变量值;value可以为二进制数据,如图片内容
  • bool $multipart: , value是否为二进制数据,发送图片等大数据时必须为真.
setReadTimeout (line 264)

设置读取超时时间,此时间必须小于SAE系统设置的时间,否则以SAE系统设置为准(默认为60秒)

  • author: zhiyong
  • access: public
void setReadTimeout (int $ms)
  • int $ms: 毫秒
setSendTimeout (line 253)

设置发送超时时间,此时间必须小于SAE系统设置的时间,否则以SAE系统设置为准(默认为20秒)

  • author: zhiyong
  • access: public
void setSendTimeout (int $ms)
  • int $ms: 毫秒

Documentation generated on Wed, 11 Aug 2010 17:34:46 +0800 by phpDocumentor 1.4.3