Php accept content type

Set HTTP Request «Content-Type»

but no luck so far.
What do I have to do to get an Content-Type: application/xml in my HTTP response?

Yes I know, but what do I need to set in my request to get an Content-Type: application/xml response?

The result you give is an HTTP response not a request. Are you asking how to set the content-type of the request, or are you asking how to ask the server to provide XML in the response?

You need to set the Accept header (and not the content-type header). Then the server needs to support multiple response types and use the accept header to determine which one to use. Do you know if the server does that?

I’m asking what do I have to set in my request to have an Content-Type: application/xml in my respons

2 Answers 2

I believe the headers must be a plain array whose elements are full key:value headers, not an associative array:

$headers = array(); $headers[] = 'Accept: application/xml'; $headers[] = 'Content-Type: application/xml'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

Is that the HTTP response back? You are sending the content-type header in your request. That doesn’t mean the responding server will send back the same content-type.

I’ve tried the same request in WizTools and the server gave back the application/xml Content-Type, so the server is capable of sending the right response. Only when I try with custom PHP, I don’t get to send the Content-Type settings.

You set an accept type for your request (and use Michael’s answer for that, you should count his answer as answering the direct question, because it does). That doesn’t mandate that the server resonds with that content type.

If the other end of the request is a static file, you have to make sure your web server sends that file with a MIME type of application/xml. If it ends with .xml, then both Apache and IIS will already do this. If it’s something else that is a non-standard file extension, but you want it to be sent as application/xml, then you’ll have to get the server manager to set the httpd.conf or .htaccess to add the mime type for the file. In IIS you use the GUI admin tools to do the same thing, adding a mime type for the file extension as application/xml.

Читайте также:  Format json with python

If the other end of the request is a server side scripting language such as PHP, Perl, Python, ColdFusion, ASP, ASP.net, etc etc then you need to use the appropriate method/function in that language for the script being called to emit the content type header and set it to application/xml.

Update: You say in comments you’re using WizTools to emit a request that does get a return of application/xml. If you want to clone that environment, then send ALL the headers it’s sending in your curl request. One guess is that user agent might be in play.

Источник

PHP: HTTP content negotiation

HTTP requests contain header that explain which data the client accepts and is able to understand: Type of the content (Accept), language (Accept-Language), charset and compression (encoding).

By leveraging this header values, your web application can automatically deliver content in the correct language. Using content types in the Accept headers, your REST API doesn’t need versioned URLs but can react differently on the same URL.

Header value structure​

Acceptance headers are comma-separated list of values with optional extension data. One additional data point — quality — determines a ranking order between the values.

Simple header​

Accept: image/png, image/jpeg, image/gif

Here the HTTP client expresses that he understands only content of MIME types image/png, image/jpeg and image/gif.

Quality​

Accept: image/png, image/jpeg;q=0.8, image/gif;q=0.5

Both image/jpeg and image/gif have a quality value now. jpeg’s 0.8 is higher than gif’s 0.5, so jpeg is preferred over gif. image/png has no explicit quality value, so the default quality of 1 is used. This means that in the end, png is preferred over jpeg, which is preferred over gif.

Читайте также:  Php invoke class function

So if the server has the data available in two formats .png and .jpeg, it should send the png file to the client.

Quality values may appear in any order:

Accept: image/gif;q=0.5, image/png, image/jpeg;q=0.8

Extensions​

Apart from the q quality extension, other tokens may be used:

Accept: text/html;video=0, text/html;q=0.9

In this example, the client prefers to get the HTML page without videos, but also falls back to the «normal» HTML page. (Note that this is an fictive example. There is no video token standardized anywhere.)

Parsing header values​

Parsing and interpreting the Accept* headers is not simply an explode() call, but you also need to strip away the extensions and order the values by their quality.

Instead of implementing this all yourself, you can rely on a the stable and unit-tested library HTTP2 from PEAR.

To use it, simply require HTTP2.php:

pecl_http​

The PHP Extension Community Library has an extension pecl_http which provides functions for HTTP content negotiation.

If you’re on a shared host, you’ll have to ask your hoster/admin to install this extension. In this case, you’re better of with the HTTP2 PEAR package since that can be installed without admin access .

Other libraries​

There are other libraries that implement HTTP content negotiation in PHP:

Content type​

The type of content is determined via the Accept header. It contains a list of MIME types that the HTTP client understands.

Apart from full MIME types, partial ones are allowed:

If you cannot provide the content type the client requests, you should return a 406 Not Acceptable HTTP status code.

Parsing the Accept header​

The HTTP2 package has full support for partial types and quality values.

 require_once 'HTTP2.php'; $http = new HTTP2(); $supportedTypes = array( 'application/xhtml+xml', 'text/html', 'application/atom+xml', 'application/json' ); $type = $http->negotiateMimeType($supportedTypes, false); if ($type === false)  header('HTTP/1.1 406 Not Acceptable'); echo "You don't want any of the content types I have to offer\n"; > else  echo 'I\'d give you data of type: ' . $type . "\n"; > ?> 

You can test the script with a command line client like curl:

$ curl -iH 'Accept: foo' http://localhost/content-type.php HTTP/1.1 406 Not Acceptable . You don't want any of the content types I have to offer 
$ curl -iH 'Accept: text/html' http://localhost/content-type.php HTTP/1.1 200 OK . I'd give you data of type: text/html 
$ curl -iH 'Accept: application/*' http://localhost/content-type.php HTTP/1.1 200 OK . I'd give you data of type: application/xhtml+xml 

Language​

HTTP clients may express the language the user understands with the Accept-Language header.

It consists of a case-insensitive list of language tags, which may be either two-letter ISO-639 language codes (like en, de, fr) or a combination of two-letter language codes and two-letter ISO-3166 country codes, separated by a dash: en-us, de-DE, de-AT.

Remember that they may include extensions and quality values.

Examples​

Accept-Language: de-DE, de;q=0.9, en-US;q=0.6, en;q=0.5

Parsing language codes​

When parsing the requested language, it is sensible to fall back to a default language. Not giving out content, because the accepted languages do not match, makes not much sense in most cases.

The following code uses a fallback language en if none of the allowed ones matches the user’s preferences.

 require_once 'HTTP2.php'; $http = new HTTP2(); $supportedLanguages = array( 'de' => 'de', 'de-DE' => 'de', 'de-AT' => 'de', 'en' => 'en', 'en-US' => 'en', 'en-UK' => 'en', ); $language = $http->negotiateLanguage($supportedLanguages, 'en'); echo 'I\'d give you text of language: ' . $language . "\n"; ?> 

Charset​

Apart from content type and language, the client may limit the accepted response charsets (what most people would call «encoding»): utf-8, iso-8859-1 — with Accept-Charset

It’s not used that often anymore, and not sent to the server by Opera 12.15, Chromium 28 and Firefox 22.

You may use HTTP2’s negotiateCharset() method to determine the preferred charset.

Links for further reading:

  • Content Negotiation: why it is useful, and how to make it work — describes a solution to use a user-configurable language setting together with language negotiation if the user did not select a language beforehand.

Источник

Оцените статью