我有疑问,是否可以先将数据发布到站点,然后获取我发布数据的站点的源代码?
我试过这个:
$html = file_get_contents('http://test.com/test.php?test=test');
但是 ?test=test 是 $_GET....
所以是的,我希望有人能帮助我! :)
提前感谢(对不起英语不好!
您可以使用此函数的第三个参数: 上下文
$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
)
);
您无法获取源代码,但可以获取服务器提供的页面/输出。正如你提到的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