//POST提交 function socketPost($url,$data){ $postStr = ''; $postLen = ''; $out = ''; //解析域 $urlInfo = parse_url($url); $host = $urlInfo['host']; if(!isset($urlInfo['query'])) $urlInfo['query'] =''; $path = $urlInfo['path'].'?'.$urlInfo['query']; //组织数据 foreach($data as $key=>$value){ $postStr .=$key.'='.rawurlencode($value).'&';#这里需要对post的值进行编码,否则会出现中断 } $postStr = trim($postStr,"&"); $postLen = strlen($postStr); $fp = fsockopen($host, 80, $errno, $errstr, 3); if ($fp) { $out .="POST ".$path." HTTP/1.0\r\n"; $out .="Host: ".$host."\r\n"; $out .= "Content-type: application/x-www-form-urlencoded\r\n"; $out .= "Content-Length: ".$postLen."\r\n"; #这里最好加上Connection: close $out .= "\r\n"; $out .= $postStr; fwrite($fp, $out); fclose($fp); } } 使用方法
代码如下 | 复制代码 | socketPost("提交的地址",array("username"=>"这里是post的username","password"=>321312312)); function socketGet($url){ $urlInfo = parse_url($url); $host = $urlInfo['host']; if(!isset($urlInfo['query'])) $urlInfo['query'] =''; $path = $urlInfo['path'].'?'.$urlInfo['query']; $fp = fsockopen($host, 80, $errno, $errstr, 3); if ($fp) { //调用模块进行抓取信息 $out = "GET {$path} / HTTP/1.1\r\n"; $out .= "Host: {$host}\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); fclose($fp); } } 使用方法:socketGet("url"); 工作需要封装好方法方便同事调用。 |
|