/* 類比資源推送類 2012-09-14 by POOY */ class TuisongPost{ //用構造登陸認證 function TuisongPost(){ //存放COOKIE的檔案 global $cookie_jar; $this->cookie_jar = tempnam('./tmp','cookie'); $url = "http://www.你的地址"; $post_data = array( "username" => "admin","password" => "admin" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_jar); //儲存cookie資訊 $output1 = curl_exec($ch); curl_close($ch); //echo $this->cookie_jar."n"; } /*得到組ID*/ function getGid($groupname,$channel,$lanmu){ $url = "http://XXXX.com/creategroup"; //格式化要推送的資料 $data = $this->getGidArr($groupname,$channel,$lanmu); $ch = curl_init(); $Ref_url = "http://www.你的地址"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $Ref_url); //偽裝REFERER curl_setopt($ch, CURLOPT_POST, 1); //post方式提交資料 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回資料,而不是直接輸出 curl_setopt($ch, CURLOPT_HEADER, 0); // 設定是否顯示header資訊 0是不顯示,1是顯示 預設為0 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //發送cookie檔案 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //發送POST資料 $output2 = curl_exec($ch); //發送HTTP請求 //這個傳回值是用作判斷的依據 return $output2; curl_close($ch); //$this->unlink($this->cookie_jar); } //推送資料 function sendPic($note,$groupid,$groupindex,$img){ $url = "http://XXXX/addimage"; $groupid = intval($groupid); $data = $this->sendPicArr($note,$groupid,$groupindex,$img); $ch = curl_init(); $Ref_url = "http://www.你的地址"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $Ref_url); //偽裝REFERER curl_setopt($ch, CURLOPT_POST, 1); //post方式提交資料 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回資料,而不是直接輸出 curl_setopt($ch, CURLOPT_HEADER, 0); // 設定是否顯示header資訊 0是不顯示,1是顯示 預設為0 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //發送cookie檔案 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //發送POST資料 $output2 = curl_exec($ch); //發送HTTP請求 return $output2 ; curl_close($ch); //$this->unlink($this->cookie_jar); } /*推送資料操作*/ function sendMes($url,$img,$imgdesc,$groupid,$groupname,$channel,$lanmu) { //var_dump($this->cookie_jar); //exit(); $url = "http://XXXX/add"; $data = $this->getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu); $ch = curl_init(); $Ref_url = "http://www.你的地址"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $Ref_url); //偽裝REFERER curl_setopt($ch, CURLOPT_POST, 1); //post方式提交資料 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回資料,而不是直接輸出 curl_setopt($ch, CURLOPT_HEADER, 0); // 設定是否顯示header資訊 0是不顯示,1是顯示 預設為0 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //發送cookie檔案 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //發送POST資料 $output2 = curl_exec($ch); //發送HTTP請求 curl_close($ch); //$this->unlink($this->cookie_jar); } function getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu) { $post_data = array( //windows使用如下寫法,linux不適用 //"img"=>"@".$img.";type=image/jpeg", "img"=>"@".$img, "imgdesc"=>$imgdesc, "groupid"=>$groupid, "groupname"=>$groupname, "channel"=>$channel, "lanmu"=>$lanmu, "cdate"=>date('Y-m-d') ); return $post_data; } //格式化getGidArr function getGidArr($groupname,$channel,$lanmu) { $post_data = array( "groupname"=>$groupname, "channel"=>$channel, "lanmu"=>$lanmu, "cdate"=>date('Y-m-d') ); return $post_data; } //格式化sendPicArr function sendPicArr($note,$groupid,$groupindex,$img) { $post_data = array( "notes"=>$note, "id"=>$groupid, "index"=>$groupindex, "cdate"=>date('Y-m-d'), //windows使用如下寫法,linux不適用 //"img"=>"@".$img.";type=image/jpeg", "img"=>"@".$img ); return $post_data; } //清理cookie檔案 function unlink($cookie_jar){ unlink($cookie_jar); } } |