以下是一个使用file_get_contents()函数发送请求并携带参数、UA和cookie的PHP代码示例:

$url = 'http://example.com/api'; // 请求的URL
$params = array( // 请求参数
  'username' => 'user1',
  'password' => 'pass1'
);
$options = array( // 请求头选项
  'http' => array(
    'method' => 'POST',
    'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36\r\n" .
                "Cookie: PHPSESSID=123456\r\n",
    'content' => http_build_query($params)
  )
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;

注意,上述示例中的User-Agent和Cookie值仅供参考,请根据实际情况替换为有效的值。另外,如果需要使用HTTPS协议发送请求,则需要在$options数组中设置ssl选项,以确保请求的安全性。

帮我写一个PHP file_get_contents 请求参数和请求头UA和cookie访问

原文地址: https://www.cveoy.top/t/topic/5eM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录