PHP中可以使用stream_context_create函数来创建一个自定义的上下文流来修改请求头。下面是一个示例代码:
// 创建一个数组来设置请求头$headers = array( 'User-Agent: MyCustomUserAgent', 'Accept: application/json');// 创建一个上下文流$context = stream_context_create(array( 'http' => array( 'header' => implode("\r\n", $headers) )));// 使用file_get_contents函数发送带有自定义请求头的请求$response = file_get_contents('http://example.com', false, $context);// 输出请求结果echo $response;在上面的示例中,首先创建了一个包含自定义请求头的数组$headers。然后使用stream_context_create函数创建一个上下文流$context,并在其中设置了请求头。最后使用file_get_contents函数发送带有自定义请求头的请求,并输出请求结果。


