溫馨提示×

PHP stream_context_create有哪些不常見的用法

PHP
小樊
124
2024-07-05 14:51:30
欄目: 編程語言

  1. 使用 stream_context_create 創建自定義的 HTTP 請求頭:
$options = array(
    'http' => array(
        'header' => "Content-Type: application/json\r\n" .
                    "Authorization: Bearer your_access_token\r\n"
    )
);
$context = stream_context_create($options);
  1. 使用 stream_context_create 設置代理服務器:
$options = array(
    'http' => array(
        'proxy' => 'tcp://proxy.example.com:8080',
        'request_fulluri' => true
    )
);
$context = stream_context_create($options);
  1. 使用 stream_context_create 設置超時時間:
$options = array(
    'http' => array(
        'timeout' => 10  // 設置超時時間為10秒
    )
);
$context = stream_context_create($options);
  1. 使用 stream_context_create 設置 SSL 驗證證書:
$options = array(
    'ssl' => array(
        'verify_peer' => true,
        'cafile' => '/path/to/ca_cert.pem'
    )
);
$context = stream_context_create($options);
  1. 使用 stream_context_create 在請求中發送文件:
$file_path = '/path/to/file.txt';
$file_contents = file_get_contents($file_path);

$options = array(
    'http' => array(
        'method' => 'POST',
        'header' => "Content-Type: application/octet-stream\r\n",
        'content' => $file_contents
    )
);
$context = stream_context_create($options);

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女