PHP 使用 stream_context_create 发送 POST 请求
文档:https://www.php.net/manual/en/function.stream-context-create.php
demo
<?php
$opts = [
'http' => [
'method' => "GET",
'header' => "Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
]
];
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
$response = stream_get_contents($fp);
fpassthru($fp);
fclose($fp);
也可以配合 file_get_contents
使用
<?php
$opts = [
'http' => [
'proxy' => 'tcp://127.0.0.1:8080',
'request_fulluri' => true,
]
];
$context = stream_context_create($opts);
$data = file_get_contents('http://www.php.net', false, $context);
echo $data;