Loving Coding & Visual Design

用HTTP_Request2替换Curl更新Twitter

由于更换服务器,不再支持Curl模块。所以决定使用Pear的HTTP_Request2包来更新更新Twitter。

原Curl代码:

$ch = curl_init($url);
if($postargs !== false){
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
}
if($this->username !== false && $this->password !== false)
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
$response = curl_exec($ch);
$this->responseInfo=curl_getinfo($ch);
curl_close($ch);
if(intval($this->responseInfo['http_code'])==200){
if(class_exists('SimpleXMLElement')){
$xml = new SimpleXMLElement($response);
return $xml;
}else{
return $response;
}
}else{
return false;
}


换为Pear::HTTP_Request2:

$req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$req->setHeader($this->headers);
if($postargs !== false){
$arr_post=explode("&",$postargs);
for($i=0,$imax=count($arr_post);$i<$imax;$i++){
$item_arr=explode("=",$arr_post[$i]);
$req->addPostParameter($item_arr[0], $item_arr[1]);
}
}
$req->setBody('');
$response = $req->send()->getBody();
if (200 == $req->send()->getStatus()) {
return $response;
}else{
return false;
}


下载解压HTTP_Request2来相同目录,如需测试,还需要下载Net_URL2PHPUnit。运行AllTests.php显示如下:
PHPUnit 3.4.6 by Sebastian Bergmann.

.................................

Time: 0 seconds, Memory: 5.00Mb

OK (33 tests, 80 assertions)

表示安装HTTP_Request2正确。



下一页


最 近 文 章

  1. Are you ready for WebGL? - Wed, 24 Feb 2010 06:04:03 +0000
  2. WebGL三维示例和Js版Box2D物理引擎 - Sun, 21 Feb 2010 03:58:44 +0000
  3. FLash与HTML5 - Fri, 05 Feb 2010 08:28:02 +0000
  4. QuickBox2D - Fri, 22 Jan 2010 09:46:02 +0000
  5. RewriteRule里正则表达式减号字符的顺序问题 - Wed, 20 Jan 2010 10:17:40 +0000
  6. Discuz7用firefox无法登录 - Tue, 19 Jan 2010 06:03:51 +0000
  7. PHP5成员重载错误: Indirect modification of overloaded property has no effect - Thu, 14 Jan 2010 05:56:26 +0000
  8. 悲哀啊,如果Google关闭谷歌中国 - Wed, 13 Jan 2010 10:40:40 +0000
  9. 用Curl登录Plesk后台备份文件 - Mon, 11 Jan 2010 04:23:46 +0000
  10. [巾*(穴/登)] - Sat, 09 Jan 2010 03:49:49 +0000