PHP GET请求,发送标头 [英] PHP GET Request, sending headers

查看:195
本文介绍了PHP GET请求,发送标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行一个get请求,并发送标头.我该怎么做?

I need to perform a get request and send headers along with it. What can I use to do this?

我需要设置的主要标题是浏览器之一.有没有简单的方法可以做到这一点?

The main header I need to set is the browser one. Is there an easy way to do this?

推荐答案

如果使用的是cURL,则可以使用curl_setopt ($handle, CURLOPT_USERAGENT, 'browser description')定义请求的用户代理标头.

If you're using cURL, you can use curl_setopt ($handle, CURLOPT_USERAGENT, 'browser description') to define the user-agent header of the request.

如果您使用的是file_get_contents,请在手册页上查看有关

If you're using file_get_contents, check out this tweak of an example on the man page for file_get_contents:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n" .
              "User-agent: BROWSER-DESCRIPTION-HERE\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

这篇关于PHP GET请求,发送标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆