Jungle Hunter
Jungle Hunter

Reputation: 7285

How to add a parameter to the request header sent by a PHP script?

I'm trying to use a web service REST API for which I need to add a parameter for authorization (with the appropriate key, of course) to get a XML result. I'm developing in PHP. How can I add a parameter to the request header in such a situation?

Edit: The way I'm doing the request right now is $xml = simplexml_load_file($query_string);

Upvotes: 1

Views: 7157

Answers (2)

Filip Roséen
Filip Roséen

Reputation: 63797

Are you using curl? (recommended)

I assume that you are using curl to do these requests towards the REST API, if you aren't; use it.

When using curl you can add a custom header by calling curl_setopt with the appropriate parameters, such as in below.

 curl_setopt (
   $curl_handle, CURLOPT_HTTPHEADER,
   array ('Authentication-Key: foobar')
 ); // make curl send a HTTP header named 'Authentication-key'
    // with the value 'foobar'

Documentation:


Are you using file_get_contents or similar?

This method is not recommended, though it is functional.

Note: allow_url_fopen needs to be enabled for file_get_contents to be able to access resources over HTTP.

If you'd like to add a custom header to such request you'll need to create yourself a valid stream context, as in the below snippet:

$context_options = array(
  'http' =>array (
    'method' => 'GET',
    'header' => 'Authentication-Key'
  )
);

$context  = stream_context_create ($context_options);

$response = file_get_contents (
  'http://www.stackoverflow.com', false, $context_options
);

Documentation:


I'm using neither of the above solutions, what should I do?

[Post OP EDIT]

My recommendation is to fetch the data using curl and then pass it off to the parser in question when all the data is received. Separate data fetching from the processing of the returned data.

[/Post OP EDIT]


Upvotes: 4

jmkeyes
jmkeyes

Reputation: 3771

When you use $xml = simplexml_load_file($query_string);, the PHP interpreter invokes it's wrapper over fopen to open the contents of a file located at $query_string. If $query_string is a remote file, the PHP interpreter opens a stream to that remote URL and retrieves the contents of the file there (if the HTTP response code 200 OK). It uses the default stream context to do that.

There is a way to alter the headers sent by altering that stream context, however, in most cases, this is a bad idea. You're relying on PHP to always open all files, local or remote, using a function that was meant to take a local file name only. Not only is it a security problem but it also could be the source of a bug that is very hard to track down.

Instead, consider splitting the loading of the remote content using cURL (checking the returned HTTP status code and other sanity checks) and then parsing that content into a SimpleXMLElement object to use. When you use cURL, you can set any headers you want to send with the request by invoking something similar to curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName' => 'value');

Hope this helps.

Upvotes: 0

Related Questions