-5

我有一个问题,是否可以先将数据发布到网站,然后获取我发布数据的网站的源代码?

我试过这个:

$html = file_get_contents('http://test.com/test.php?test=test');

但是 ?test=test 是 $_GET....

所以,是的,我希望有人能帮助我!:)
在此先感谢(抱歉英语不好!)。

4

2 回答 2

1

您可以使用此函数的第三个参数:上下文

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array(
    'http' => array(
        'method' => "POST",
        'header' => "Connection: close\r\n".
                        "Content-Length: ".strlen($postdata)."\r\n",
        'content' => $postdata
  )
);

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

// 编辑 - 小错误应该是:

$opts = array(
    'http' => array(
        'method' => "POST",
        'header' => "Connection: close\r\n".
                    "Content-type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($postdata)."\r\n",
        'content' => $postdata
  )
);
于 2012-06-25T19:11:28.687 回答
0

您无法获取源代码,但可以获取服务器提供的页面/输出。正如您提到file_get_contents()的,您可以使用它来发送 POST 请求,但它看起来像这样。

// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');

// Build Http query using params
$query = http_build_query ($params);

// Create Http context details
$contextData = array ( 
                'method' => 'POST',
                'header' => "Connection: close\r\n".
                            "Content-Length: ".strlen($query)."\r\n",
                'content'=> $query );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));

// Read page rendered as result of your POST request
$result =  file_get_contents (
                  'http://www.sample-post-page.com',  // page url
                  false,
                  $context);

// Server response is now stored in $result variable so you can process it

示例来自: http: //fczaja.blogspot.se/2011/07/php-how-to-send-post-request-with.html

于 2012-06-25T19:07:43.933 回答