PHP에서 file_get_contents를 사용하여 여러 쿠키를 보내는 방법은 무엇입니까?

DDD
풀어 주다: 2024-10-18 08:44:03
원래의
472명이 탐색했습니다.

How to Send Multiple Cookies with file_get_contents in PHP?

Sending Multiple Cookies with file_get_contents

The PHP manual provides an example demonstrating how to send a cookie using stream contexts. However, it does not address the scenario of sending multiple cookies.

Multiple Cookie Options

There are several options for sending multiple cookies:

  • Option 1:
<code class="php">"Cookie: user=3345&pass=abcd\r\n"</code>
로그인 후 복사

This option combines the cookies into a single string, separated by an ampersand (&). However, it may not be compatible with all servers.

  • Option 2:
<code class="php">"Cookie: user=3345\r\n" . 
"Cookie: pass=abcd\r\n"</code>
로그인 후 복사

This option sends the cookies as separate lines with individual Cookie headers. It provides better compatibility but may look messy.

Optimal Solution

The preferred option for sending multiple cookies is:

  • Option 3:
<code class="php">Cookie: user=3345; pass=abcd</code>
로그인 후 복사

This syntax uses semicolons (;) to separate the cookie pairs. It is widely supported and follows the HTTP cookie specification.

Implementation

To send multiple cookies using file_get_contents:

<code class="php">$cookies = array('user' =&gt; '3345', 'pass' =&gt; 'abcd');
$cookieString = '';
foreach ($cookies as $name =&gt; $value) {
    $cookieString .= "$name=$value;";
}

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

$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com/', false, $context);</code>
로그인 후 복사

위 내용은 PHP에서 file_get_contents를 사용하여 여러 쿠키를 보내는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!