1

我正在尝试获取 POST 结果,但服务器已阻止它。我试过了

  • fsockopen
  • 卷曲
  • 文件获取内容

但是我从服务器得到的结果与“拒绝访问”的错误消息相同。

有什么方法可以从块服务器获取 POST 结果。

<?php
$post_arr = array ("regno" => "1"); 
    $addr = 'url'; 

    $fp = fsockopen($addr, 80, $errno, $errstr, 30); 
    if (!$fp) { 
        echo "$errstr ($errno)<br />\n"; 
    } else { 

        $req = ''; 
        foreach ($post_arr as $key => $value) { 
            $value = urlencode(stripslashes($value)); 
            $req .= "&" . $key . "=" . $value; 
        } 


        $header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; 
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
        $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 
        fwrite($fp, $header); 
        while (!feof($fp)) { 
            echo fgets($fp, 128); 
        } 
        fclose($fp); 
    }  
?>

并且

<?php
    $postdata = http_build_query( 
        array( 
            'regno' => 1
        ) 
    ); 

    $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('url', false, $context); 
    echo $result;
?>

上述两种方法都给了我拒绝访问的输出。

4

1 回答 1

2

尝试设置 HTTP_REFERER 变量。

'header'  => "Content-type: application/x-www-form-urlencoded\r\nReferer: http://urltopost\r\n",

如果这不起作用,请尝试模仿更多的标题。这是我通过查看 Chrome 开发者工具的网络选项卡得到的:

POST /hse/result.asp HTTP/1.1
Host: urlhost
Connection: keep-alive
Content-Length: 17
Cache-Control: max-age=0
Origin: url
User-Agent: something
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: url
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASPSESSIONIDASRCTADA=FBHLDODAOCBACEKMNFLJIMGO

编辑以包含工作代码:如果您将第二个代码片段中的 $opts 定义替换为此,它将起作用。这个对我有用。

$opts = array('http' => 
    array( 
        'method'  => 'POST', 
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n"
                    ."Referer: url\r\n",
        'content' => $postdata 
    ) 
); 

完整代码:

<?php 
$postdata = http_build_query( array( 'regno' => 1 ) ); 
$opts = array('http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" ."Referer: urltopost\r\n", 'content' => $postdata ) );
$context = stream_context_create($opts); 
$result = file_get_contents('urltopost', false, $context);
echo $result;
?>
于 2012-05-14T15:22:19.767 回答