There are some web service APIs that I need to connect to for my website. Most of the APIs involve something like this:
JavaScript
x
$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:
JavaScript
<?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);
?>