我有疑问,是否可以先将数据发布到网站,然后获取我发布数据的网站的源代码?
我试过这个:
$html = file_get_contents('http://test.com/test.php?test=test');
但是?test = test是$ _GET ....
所以是的,我希望有人可以帮助我! :)
提前致谢(抱歉英语不好!)。
答案 0 :(得分:1)
您可以使用此功能的第3个参数: context
$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);
//编辑-little bug应该是:
$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
)
);
答案 1 :(得分: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