There are some web service APIs that I need to connect to for my website. Most of the APIs involve something like this:
$data = file_get_contents("http://www.someservice.com/api/fetch?key=1234567890
But one web service requires the API key to be set in a custom HTTP header. How do I make the request to this API url and pass the custom header at the same time?
Advertisement
Answer
You could use stream_context_create like this:
<?php $options = array( 'http'=>array( 'method'=>"GET", 'header'=>"CustomHeader: yayrn" . "AnotherHeader: testrn" ) ); $context=stream_context_create($options); $data=file_get_contents('http://www.someservice.com/api/fetch?key=1234567890',false,$context); ?>