POST和GET没有cURL PHP

时间:2014-01-14 03:34:36

标签: php curl file-get-contents

我曾经尝试过在这个网站http://www.sefaz.pe.gov.br/sintegra发布POST和GET,但它不起作用

// POST
$data = array('CNPJ' => '02340534000192', 'consulta' => 'OK', 'vazio' => '');
$html = $this->HTTP_Post('http://www.sefaz.pe.gov.br/sintegra/consulta/consulta.asp',    $data, $cookie);
$arr = split("Set-Cookie:", $html); 
$cookie="";
$count=1; 
while ($count < count($arr)) 
{ 
 $cookie.=substr($arr[$count].";", 0, strpos($arr[$count].";",";")+1); 
 $count++;
} 

// GET after POST
echo $this->get_url('http://www.sefaz.pe.gov.br/sintegra/consulta/exibirResultado.asp', $cookie);

我尝试了file_get_contents并且卷曲了......两次都没有成功

function get_url($url,$cookie=false)
{
  $url = parse_url($url);
  $query = $url[path]."?".$url[query];
  echo "Query:".$query;
  $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
  if (!$fp) 
  {
    return false;
  }
  else 
  {
    $request = "GET $query HTTP/1.1\r\n";
    $request .= "Host: $url[host]\r\n";
    $request .= "Connection: Keep-Alive\r\n";
    if($cookie) $request.="Cookie:   $cookie\n";
    $request.="\r\n";
    fwrite($fp,$request);

    while (!feof($fp)) 
    {
      $result .= fgets($fp, 128);
   }

   fclose($fp);
   return $result;
  }
}

function HTTP_Post($URL,$data,$cookie, $referrer="")
{
  $URL_Info=parse_url($URL);
  if($referrer=="") $referrer="111";
  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.="Connection: Keep-Alive\n";
  $request.="Cookie:   $cookie\n";
  $request.="\n";
  $request.=$data_string."\n";
  $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  fputs($fp, $request);
  while(!feof($fp)) 
  {
    $result .= fgets($fp, 1024);
  }
  fclose($fp);
  return $result;
}

使用fsocket进行GET和POST功能。 欢迎任何想法

由于

编辑1:

谢谢,但它不起作用。我尝试了20次,但这个网站不起作用......

出现浏览器消息:Atenção:Acesso negado!赞成selecionaralgumparâmetrodeconsulta。

警告:拒绝访问!请选择查询参数。

我的代码与你的代码

function POST()
{
  $postdata = http_build_query(
    array(
          'CNPJ' => '02340534000192',
          'consulta' => 'OK',
          'vazio' => '',
          'CPF' => '',
          'IE' => '',
          'razaosocial' => ''
         ));

   $headers = array(
                         'Content-type: application/x-www-form-urlencoded',
                         'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                        );

  $opts = array('http' =>
    array(
          'method'  => 'POST',
          'header'  => implode("\r\n", $headers),
          'content' => $postdata,
         ));

  $context  = stream_context_create($opts);
  $result = file_get_contents('http://www.sefaz.pe.gov.br/sintegra/consulta/consulta.asp', false, $context);
  return $result;
}

2 个答案:

答案 0 :(得分:2)

我认为你可以为GET做file_get_contentsPOST$postdata = http_build_query( array( 'var1' => 'some content', 'var2' => 'doh' ) ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $context = stream_context_create($opts); $result = file_get_contents('http://example.com/submit.php', false, $context);

来自php.net网站的示例

{{1}}

答案 1 :(得分:0)

您的IP可能已被网站阻止。