如何使用php';s stream_context_create()?

如何使用php';s stream_context_create()?,php,http-headers,file-get-contents,Php,Http Headers,File Get Contents,我需要帮助为文件获取内容请求定义$opts变量 为此,您应该这样定义您的上下文: $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ) ); $context = stream_context_create(

我需要帮助为文件获取内容请求定义$opts变量

为此,您应该这样定义您的上下文:

    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
      )
    );

$context = stream_context_create($opts);
$file = file_get_contents($url, false, $context);
如何将以下标题格式化为上面的$opt变量

GET /oncorev2/DetailTop.aspx?ref= HTTP/1.1
Host: subdomain.domain.org
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/24.0.3215.75 Safari/537.36
Referer: https://subdomain.domain.org/oncorev2/ShowDetails.aspx?CFN=98664975
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=wptjainh1n40er45inkk3v45; OnCoreWeb=AutoLoadImages=-1&ImageViewer=2&DefaultNumberOfRows=10
我试过几种不同的方法,但都错了。这是我尝试过的一个例子

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"GET /oncorev2/DetailTop.aspx?ref= HTTP/1.1
Host: subdomain.domain.org
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/24.0.3215.75 Safari/537.36
Referer: https://subdomain.domain.org/oncorev2/ShowDetails.aspx?CFN=104525097
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8\r\n
Cookie:ASP.NET_SessionId=lrv3rfjl1ay0aj45v33s0ozm;OnCoreWeb=AutoLoadImages=-1&ImageViewer=2&DefaultNumberOfRows=10"
  ));
谢谢

修订: 正如评论中提到的,我现在也尝试了

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>array("Host: subdomain.domain.org",
"Connection: keep-alive",
"Cache-Control: max-age=0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/24.0.3215.75 Safari/537.36",
"Referer: https://subdomain.domain.org/oncorev2/ShowDetails.aspx?CFN=104525097",
"Accept-Encoding: gzip, deflate, sdch",
"Accept-Language: en-US,en;q=0.8\r\n",
"Cookie:ASP.NET_SessionId=lrv3rfjl1ay0aj45v33s0ozm;OnCoreWeb=AutoLoadImages=-1&ImageViewer=2&DefaultNumberOfRows=10")
  ));

只需使用一个数组,例如
'header'=>数组(“接受语言:en”,“Cookie:foo=bar”)
如果我看起来很笨,请原谅。我正在使用数组。您能再解释一下吗?您的header元素当前是一个字符串。而不是
'header'=>'header1'header2'
,如上所述将元素转换为数组我可能不理解您的意思。我表明了我对你所说的话的理解,这一修改现在反映在我的帖子中。它不起作用,但告诉我是否接近。只要使用一个数组,例如
'header'=>数组(“接受语言:en”,“Cookie:foo=bar”)
如果我看起来很笨,请原谅我。我正在使用数组。您能再解释一下吗?您的header元素当前是一个字符串。而不是
'header'=>'header1'header2'
,如上所述将元素转换为数组我可能不理解您的意思。我表明了我对你所说的话的理解,这一修改现在反映在我的帖子中。这不管用,但告诉我我是否快到了。