ling凌yue月 发表于 2013-2-7 02:02:05

php curl post数据类

 
class PostData{private $postURL; //要请求的url地址private $postData; //提交的数据 key=>value方式/** * 表示应答信息 * @var unknown_type */private $responseBody; /** * 表示应答头信息 * @var unknown_type */private $responseHead; /** ** @var unknown_type */private $errMessage = ''; //返回的错误信息public $CRLF = "\r\n";public $sCookieFile = 'E:/tmp.cookie';/*** 构造方法** @author * @param 要请求的url地址* @param 要提交的数据 */function __construct($url='', $data=''){$this->setPostURL($url);$this->setPostData($data);}/*** 设置url的值** @author * @param 要请求的url地址 */public function setPostURL($url){$this->postURL = $url;}/*** 设置data的值** @author * @param 要提交的数据 */public function setPostData($data){$this->postData = $data;}/** * * 返回错误信息** @author * @param type 说明* @return 返回说明 */public function getErrMessage(){return $this->errMessage;}/*** 返回应答信息** @author * @param type 说明* @return 返回说明 */public function getResponseBody(){return $this->responseBody;}/*** 返回应答头信息** @author * @param type 说明* @return 返回说明 */public function getResponseHead(){return $this->responseHead;}/** * <br /> * <span><strong>此方法已过期,不建议用,请用curlpost</strong></span><br /><br />* 将$this->data提交到$this->url** @author * @param postType 提交形式,post或是get* @param extensionalHeader 扩展的头文件。格式:array('Host' => 'Host: www.example.com','Connection' => 'Connection: close')* @return 返回的数据**/public function sendpost($postType='POST' ,$extensionalheader = array()){$url = $this->postURL;$data = $this->postData == '' || !isset($this->postData) ? array() : $this->postData;//先解析url$url = parse_url($url);$url_port = !isset($url['port']) ? (($url['scheme'] == 'http') ? 80 : 443) : $url['port'];if (!$url){$this->errMessage = "couldn't parse url";return false;}//将参数拼成URL key1=value1&key2=value2 的形式$encoded = "";while (list($k, $v) = each($data)){$encoded .= ($encoded ? '&' : '');$encoded .= rawurlencode($k)."=".rawurlencode($v);}//echo $encoded;exit;$len = strlen($encoded);//拼上http头$out = "{$postType} ".$url['path'].(isset($url['query']) ? ('?'.$url['query']) : '')." HTTP/1.1".$this->CRLF;$host = "Host:".$url['host'];$content_type = 'Content-type: application/x-www-form-urlencoded';$connection = 'Connection: close';$iheads = array('Host' => $host,'Content-type' => $content_type,'Connection' => $connection,);$heads = array_merge($iheads, $extensionalheader);foreach ($heads as $k => $v){$out .= $v.$this->CRLF;}$out .= "Content-Length: $len".$this->CRLF;$out .= $this->CRLF;$out .= $encoded.$this->CRLF;//打开一个sock$fp = @fsockopen($url['host'], $url_port);$line = "";if (!$fp){$this->errMessage = "{$errstr}({$errno})\n";return false;}else{fwrite($fp,$out);while (!feof($fp)){$line .= fgets($fp, 2048);}}if ($line){$head = substr($line, 0, strpos($line, $this->CRLF.$this->CRLF));$body = stristr($line, $this->CRLF.$this->CRLF);$body = substr($body, 4, strlen($body));}fclose($fp);$this->responseHead = $head;$this->responseBody = $body;return $line;}/*** * 用curl向指定url提交数据** @author * @param $sendType str 提交类型 【可选】* @param $extensionalheader array 附加头 【可选】* @param $sendCookieFile boolean 是否发送cookie文件的路径 【可选】 默认路径为 tmp.cookie* @param $saveCookieFile boolean 是否保存cookie文件的路径 【可选】默认路径为 tmp.cookie* @param $cookie string 要发送的cookie字符串比sendCookie参数优先级低如果已有sendCookie 则此选项无效【可选】* @return 返回 返回的数据 */public function curlpost($sendType = 'POST' ,$extensionalheader = array(), $sendCookieFile = false, $saveCookieFile = false, $cookie = false){$url = $this->postURL;$data = $this->postData;$aurl = parse_url($url);$encoded = "";//$sCookieFile = tempnam('.','cookie');while (list($k, $v) = each($data)){$encoded .= ($encoded ? '&' : '');$encoded .= rawurlencode($k)."=".rawurlencode($v);}$ch = curl_init();//初始化CURL句柄curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URLcurl_setopt($ch, CURLOPT_HEADER, 1);//*-----------------设置发送cookieif ($sendCookieFile){curl_setopt($ch, CURLOPT_COOKIEFILE, $this->sCookieFile);}else if ($cookie){curl_setopt($ch, CURLOPT_COOKIE, $cookie);}//设置保存cookieif ($saveCookieFile){curl_setopt($ch, CURLOPT_COOKIEJAR, $this->sCookieFile);}//-------------------------------*/curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 设为TRUE把curl_exec()结果转化为字串,而不是直接输出if ($sendType != 'GET'){curl_setopt($ch, CURLOPT_POST, 1);//启用POST提交curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); //设置POST提交的字符串}$host = $aurl['host'];$iheads = array('Host' => $host,'Content-type' => 'Content-type: application/x-www-form-urlencoded','Connection' => 'Connection: close');$heads = array_merge($iheads, $extensionalheader);curl_setopt($ch, CURLOPT_HTTPHEADER, $heads);//设置HTTP头信息//curl_setopt($ch, CURLOPT_COOKIEFILE, $sCookieFile);//从$sCookieFile文件里得到信息//file_put_contents($sCookieFile, '');//curl_setopt($ch, CURLOPT_COOKIEJAR, $sCookieFile);//保存cookie信息到$sCookieFile文件里$document = curl_exec($ch);//执行预定义的CURL$this->errMessage = curl_error($ch);$info = curl_getinfo($ch);curl_close($ch);$this->responseHead = substr($document, 0, $info['header_size']);$this->responseBody = substr($document, $info['header_size']);return $document;}/** * * 清除cookie文件中的内容** @author * @param type 说明* @return 返回说明 */function clearCookie(){file_put_contents($this->sCookieFile, '');}}
页: [1]
查看完整版本: php curl post数据类