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);
Are you using curl? (recommended)
I assume that you are using
curlto do these requests towards the REST API, if you aren’t; use it.When using
curlyou can add a custom header by callingcurl_setoptwith the appropriate parameters, such as in below.Documentation:
Are you using file_get_contents or similar?
This method is not recommended, though it is functional.
Note:
allow_url_fopenneeds to be enabled forfile_get_contentsto 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:Documentation:
I’m using neither of the above solutions, what should I do?
[Post OP EDIT]
My recommendation is to fetch the data using
curland 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]