如何将POST数据设置为URL并在字符串变量中获取结果 [英] How to set POST data to URL and get results in string variable

查看:104
本文介绍了如何将POST数据设置为URL并在字符串变量中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取POST结果,但服务器已将其阻止. 我已经尝试过

I am trying to get a POST result but the server has block it. I have tried with

  • fsockopen
  • 卷曲
  • file_get_contents

但是我从服务器得到的结果与访问被拒绝"的错误信息相提并论.

but I got a same result from the server as an eroor massage of "Access denied" .

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

Is there any way to get the POST results from the block server.

<?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;
?>

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

Both the above method give me a access denied output.

推荐答案

尝试设置HTTP_REFERER变量.

Try setting the HTTP_REFERER variable.

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

如果这不起作用,请尝试模仿更多的标头.这是我从Chrome开发者工具的网络"标签中看到的:

And if that doesn't work, try to mimic more of the headers. This is what I have from looking at the Network Tab of Chrome Developer Tools:

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定义替换为此,它将起作用.它对我有用.

Edited to include working code: If you replace the $opts definition in your second code snippet with this, it will work. It works for me.

$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;
?>

这篇关于如何将POST数据设置为URL并在字符串变量中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆