fsockopen - POST和GET PHP

时间:2014-01-17 07:08:34

标签: php post cookies get fsockopen

使用fsockopen执行POST后,我的 GET 请求出现问题。我发送了Cookie,但没有成功。

我收到 HTTP / 1.1 302移动对象 - >的 POST
我收到 HTTP / 1.1 302对象移动 - > GET ,但我希望收到 HTTP / 1.1 200 OK

我的代码:

用法: $ this-> read();

function HTTP_POST($URL, $data, $referrer="") 
{ 
  $URL_Info=parse_url($URL); 
  if($referrer=="")
  {
    $referrer=$_SERVER["SCRIPT_URI"]; 
  }

  foreach($data as $key=>$value) 
  {
    $values[]="$key=".urlencode($value); 
  }
  $data_string=implode("&",$values); 

  if(!isset($URL_Info["port"])) 
  {
    $URL_Info["port"]=80; 
  }

  $request.="POST ".$URL_Info["path"]." HTTP/1.1\n"; 
  $request.="Host: ".$URL_Info["host"]."\n"; 
  $request.="Referer: $referer\n"; 
  $request.="Content-type: application/x-www-form-urlencoded\n"; 
  $request.="Content-length: ".strlen($data_string)."\n"; 
  $request.="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2100101 Firefox/4.0.1\n";
  $request.="Connection: close\n"; 
  $request.="\n"; 
  $request.=$data_string."\n"; 

  $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); 
  fputs($fp, $request); 
  while(!feof($fp)) 
  { 
    $result .= fgets($fp, 128); 
  } 
  fclose($fp); 
  return $result; 
} 

function HTTP_GET($URL, $cookie, $referrer="")
{ 
  $URL_Info = parse_url($URL); 
  if($referrer=="") 
  {
    $referrer=$_SERVER["SCRIPT_URI"]; 
  }
  if(!isset($URL_Info["port"]))
  {
    $URL_Info["port"]=80; 
  }

  $request.="GET ".$URL_Info["path"]." HTTP/1.1\n"; 
  $request.="Host: ".$URL_Info["host"]."\n"; 
  $request.="Referer: $referer\n"; 
  $request.="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2100101 Firefox/4.0.1\n";
  $request.="Cookie: ".$cookie."\n"; 
  $request.="Connection: close\n"; 
  $request.="\n"; 

  $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); 
  fputs($fp, $request); 
  while(!feof($fp)) 
  { 
    $result .= fgets($fp, 128); 
  } 
  fclose($fp); 
  return $result; 
}


function read()
{
  $headers = array();
  $data = array('CNPJ' => '02340534000192', 'consulta' => 'OK', 'vazio' => '');
  $html = $this->HTTP_POST('http://www.sefaz.pe.gov.br/sintegra/consulta/consulta.asp', $data);

  $arr = split("Set-Cookie:", $html); 
  $cookie="";
  $count=1; 
  while ($count < count($arr)) 
  { 
    $cookie.=substr($arr[$count].";", 0, strpos($arr[$count].";",";")+1); 
    $count++;
  } 

  echo $html."<br><hr><br>"; 

  $html = $this->HTTP_GET('http://www.sefaz.pe.gov.br/sintegra/consulta/exibirResultado.asp', $cookie);
  echo $html;
}

任何人都可以帮助我吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

HTTP / 1.1 302对象移动意味着页面临时从该位置移动。 再次检查您的请求。尝试从浏览器打开请求页面。