如何使用php'的stream_context_create()格式化请求标头?

时间:2015-12-05 21:15:04

标签: php http-headers file-get-contents

我需要帮助为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);
$file = file_get_contents($url, false, $context);

如何将以下标题格式化为上面的$ opt&#39变量?

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"
  ));

由于

REVISION: 正如评论中所建议的,我现在也尝试了

$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")
  ));

0 个答案:

没有答案