Html header for xml

Create header of xml file with php

PHP header is an inbuilt function that is used to send a raw HTTP header to the client and it is mandatory that they actually manipulate the information which is sent to the client or browser before any original output can be sent. Solution 2: I’d try this method instead: Introduction to PHP header()

Create header of xml file with php

I want to create the header of a xml saved file http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd»> , but I get error message

$uru = "urlset xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'"; $xml = new DOMDocument("1.0"); $xml->formatOutput=true; $urlset=$xml->createElement("$uru"); $xml->appendChild($urlset); 
PHP Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in /blablabla/blabla/file.php:11 Stack trace: #0 /blablabla/blabla/file.php(11): DOMDocument->createElement('urlset xsi:sche. ') #1 thrown in /blablabla/blabla/file.php on line 11 
$urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); 

Also you need an element to append it to.
Here is an example on how your code should look like

$xml = new DOMDocument("1.0"); $urlset = $xml->createElement('SetURL'); $urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); $xml->appendChild($urlset); 

A little sitenote:
You should also set your xmlns and xmlns:xsi and not only the xsi:schemaLocation.
In your case when working with sitemaps you should set theese attributes then.

$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); 

This is for the XMLNS (here is the source)

$urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); 

And for the XMLNS:XSI(Same source)

I’d try this method instead:

$xml = new SimpleXMLElement(''); $uru = "urlset xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'"; $var = $xml->addChild($uru); Header('Content-type: text/xml'); print($xml->asXML()); 

PHP header for xml with utf-8, header just sets a HTTP header in the result. PHP doesn’t do anything else with the value, so it’s up to you to make sure it’s being used properly. If you’re using an XML library to generate your XML (including the prologue), check the documentation for that library. Usage example$doc = new DomDocument(‘1.0’, ‘UTF-8’);Feedback

PHP header()

Introduction to PHP header()

PHP header is an inbuilt function that is used to send a raw HTTP header to the client and it is mandatory that they actually manipulate the information which is sent to the client or browser before any original output can be sent. A raw request (like an HTTP request) is sent to the browser or the client before HTML, XML, JSON or any other output has been sent. HTTP headers contain required necessary information of the object which is sent in the message body more accurately on the request and its response.

Syntax and Parameter

Below are the syntax and parameter:

void header( $header_name, $replace_param = TRUE, $http_response_code )
header_name() here is a mandatory field and sends the header string to be sent

replace parameter: It is an optional boolean type field and shows if the present header should replace a previous similar-looking header or should add a new header which is of the same type. By default its value is TRUE and by this, it replaces the header unless given FALSE which allows giving multiple headers but the condition is that it should have the same type.

Читайте также:  Python checking directory exist

header(‘WWW-Authenticate: Negotiate’);
echo (‘header has been changed to WWW-Authenticate: Negotiate’);
echo «\n»;
header(‘WWW-Authenticate: NTLM’, false);
echo (‘header has been changed to WWW-Authenticate: NTLM’);
?>

PHP header() - 1

http_response_code: It is also an optional field which forces the HTTP response received to the given value. Available in versions PHP 4.3 and higher.

The header() here is used to send a raw HTTP header. This header hence must be called before any other output is been sent either by usual HTML tags, blank lines or from PHP. A few common mistakes are to read the code with include, access or any other require functions, having spaces or empty lines which are output before calling the header(). This problem also exists when we are using an individual PHP or an HTML file.

Return Values : header() function does not return any value. In header calls, there are 2 types: The first one starts with the string “HTTP/” (case insignificant) which is used to find out the HTTP status code to send.

Examples to Implement PHP header()
Example #1

header(«HTTP Error 404: Not Found»);
echo (‘Header been changed to HTTP Error 404: Not Found’);
?>

PHP header() - 2

Explanation: The second type is the Location header which sends the header back to a web browser and also returns back a REDIRECT status code to the browser until and unless status codes 201 or 3xx have been already sent.

Example #2

header(«Location: http://www.example_site.com/»);/* This is to redirect the browser */
echo(«Browser successfully redirected to http://www.example_site.com/»);
exit;
?>

PHP header() - 3

Example #3

// To output a PDF header
header(‘Content-Type: application/pdf’);
// Let the filename be called file.pdf
header(‘Content-Disposition: attachment; filename=»file.pdf»‘);
// The source of PDF can be found in oldfile.pdf
readfile(‘oldfile.pdf’);
?>

Content-Disposition

Explanation: In this example, we are prompting the user to save the generated PDF file being sent to them. For this purpose, we use the Content-Disposition header to give a required file name and to force the web browser to show the save dialog.

Example #4

header(«Cache-Control: no-cache, hence should-revalidate»);
header(«Expires: Sun, 25 Jun 1999 04:00:00 GMT»);
// Providing some random date in the past
echo(‘Displaying header information: Cache-Control: no-cache, hence should-revalidate’ );
?>

proxies

Explanation: In this example, we are using certain proxies and clients to disable the caching process of PHP. This is because PHP often creates dynamic content that should not be cached by the web browser or any other proxy caches which come in between server and browser.

Читайте также:  Find if python is 64 bit

Sometimes it may happen that the pages will not be cached even if the above said lines and headers are not incorporated in the PHP code. This is because a lot of options are available which a user can set for his browser that actually changes its default set caching behavior. Hence by using the above-mentioned headers we will be able to override all the settings which may cause the output of PHP script to be cached.

There is also another configuration setting called the session.cache_limiter which generates the correct cache-related headers automatically when different sessions are being used.

Example #5

// PHP program to describes header function
// Set a past date
header(«Expires: Sun, 25 Jul 1997 06:02:34 GMT»);
header(«Cache-Control: no-cache»);
header(«Pragma: no-cache»);
?>

Hello World!

header list —>
print_r(headers_list());
?>

prevent caching

Explanation: The above-given example is used to prevent caching which sends the header information to override the browser setting so that it does not cache it. We are using the header() function multiple times in this example as only one header is allowed to send at one time. This prevents something called header injection attacks.

Example #6

redirect

Explanation: This example above is used to redirect the user and to inform him that he will be redirected.

Example #7

// Test image.
$t1 = ‘https://cdn.educba.com/test/image.png’;
// Headers being sent by the client
$headers = apache_request_headers();
// To check if the cache is being validated by the client and whether it is current
if (isset($headers[‘If-Modified-Since’]) && (strtotime($headers[‘If-Modified-Since’]) == ftime($t1))) <
// We shall respond with ‘304 Not Modified’ if the client cache is current
header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s’, ftime($t1)).’ GMT’, true, 304);
echo(‘’);
> else <
// We shall display ‘200 OK’ by outputting the image if it is not cached or outdated cache
header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s’, ftime($t1)).’ GMT’, true, 200);
header(‘Content-Length: ‘.filesize($t1));
header(‘Content-Type: image/png’);
print file_get_contents($t1);
>
?>

IS current

Explanation: In the above example, we are using php headers to cache an image being sent and hence bandwidth can be saved by doing this. First, we take the image and check if it is already cached, this by setting the cache to IS current. If it is not current then we are caching the same and sending the image in the output.

Advantages of using the header function in PHP
  • PHP headers are very essential in redirecting the URI string also to display the appropriate message such as “404 Not Found Error”.
  • PHP headers can be used to tell the web browser what type the response is, and the content type.
  • The redirect script which will be used at the beginning helps in saving time of execution and bandwidth.
Читайте также:  Install wheel python windows
Conclusion

As seen above, the header forms a major part of each document as it tells the web browser what kind of document it is so that the browser reads it correctly. Hence it is responsible for providing raw HTTP headers to browsers and also to redirect the same to different proper locations.

Final thoughts

This is a guide to PHP header(). Here we discuss an Introduction to PHP header() along with appropriate Syntax, and top 7 examples to implement with proper codes and outputs.

PHP header() | Complete Guide to PHP header() with, PHP headers are very essential in redirecting the URI string also to display the appropriate message such as “404 Not Found Error”.PHP headers can be used to tell the web browser what type the response is, and the content type.The redirect script which will be used at the beginning helps in saving time of e… PHP headers are very essential in redirecting the URI string also to display the appropriate message such as “404 Not Found Error”.PHP headers can be used to tell the web browser what type the response is, and the content type.The redirect script which will be used at the beginning helps in saving time of execution and bandwidth.

PHP Post with Header and XML Content

I want to subscribe a YouTube Channel through the GData API via PHP.

Documentation

How can i do this post in PHP?

I tried it like this but the page keeps loading forever:

 $xml ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); return $result; > $token = $_SESSION['token']; $xml = '   GoogleDevelopers '; $url = 'https://gdata.youtube.com/feeds/api/users/default/subscriptions'; echo post_xml($url, $xml); ?> 

In HttpRequester i already managed it to do a HTTP Post Request and it worked. I think the problem is the content of the Request. How do i properly give the text which is in «Content» (look at the screenshot) via PHP (cURL)?

HttpRequester

Just put the $xml as POSTFIELDS and it should work:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 

And add the correct content-length:

When you’re planning to do more, like PUT-requests with data and just lots of REST-requests, I would suggest to use some kind of package like Httpful. Curl has it’s pitfalls.

You can look at the code below, hope it will help

 $headers = array( "Content-type: text/xml", "Content-length: " . strlen($xml), "Connection: close", ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $res = curl_exec($ch); curl_close($ch); return json_decode($res); 

Create header of xml file with php, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Источник

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