I'm trying to parse an xml feed using PHP:
http://trustbox.trustpilot.com/r/travelnation.co.uk.xml
Visiting this, it looks perfectly OK, but when I try
<?php
$file = file_get_contents("http://trustbox.trustpilot.com/r/netamity.com.xml");
print_r($file);
?>
I get
‹•SÁŽÓ0=/ÿ`ŒÄmœ- 븊àèJV«••L«ŽmÙN²ý{Æi·M
...
How is it getting garbled? Using simplexml it wont parse it (unsurprisingly). I've tried setting headers UTF-8 headers but I think the issue is in the get_file_contents. Any ideas?
The content looks "weird" simply because the encoding is compressed (see the HTTP header Content-Encoding: gzip).
HTTP/1.1 200 OK
x-amz-id-2: 8wYarFnod0jtLJ3U8ZDN38102fjtG+EbwJjy0tY4YTZncrz9auEcQbzt1vyiSEhq
x-amz-request-id: A60F1E6CA5437776
Date: Sun, 24 Feb 2013 18:00:45 GMT
Content-Encoding: gzip
Last-Modified: Sun, 24 Feb 2013 05:19:11 GMT
ETag: "64eaa6f87768aeb3ae6741ba06318cb6"
Accept-Ranges: bytes
Content-Type: application/xhtml+xml
Content-Length: 52366
Server: AmazonS3
I guess what you need is to know how to read a file over HTTP; you could try this one on SO.
Related
My PHP code returns a JSON string and sets to the response header Content-Type: application/json.
On localhost it works fine, it just returns the JSON string I passed. But, request to the remote server, on my browser network/header, the content type is application/x-httpd-php5. And the content, before the json string, it prints the texts in the std-output and that header string:
X-Powered-By: PHP/7.2.8
Cache-Control: private, must-revalidate
Date: Wed, 08 Aug 2018 02:49:36 GMT
Content-Type: application/json
pragma: no-cache
expires: -1
X-Debug-Token: 187f4e
after that I get the json string I sent.
The PHP version of my PC and the hosting is PHP 7.2. I use Apache. The hosting supposed too. But, from the browser response header, I see Server: nginx.
From the server into Mime types, I got application/json -> json. I uploaded a .json file, I got Content-Type: application/json. I set to my .htaccess file
AddDefaultCharset utf-8
AddType 'application/json; charset=UTF-8' .json
but still nothing changes.
I don't know why on my localhost I got Content-Type: application/json but on the hosting I got application/x-httpd-php5.
Here is the raw Response:
HTTP/2.0 200 OK
server: nginx
date: Wed, 08 Aug 2018 03:31:45 GMT
content-type: application/x-httpd-php5
cache-control: max-age=0
expires: Wed, 08 Aug 2018 03:31:44 GMT
X-Firefox-Spdy: h2
I have the same website with the same css file being gzipped and served on two separate servers. Viewing the site on one server, the browser properly decompresses it, and uses the styling. But on the other, the browser does not decompress the file. I thought perhaps this is something to do with the headers, but all the resources I've found seem to think the Content-Type and Content-Encoding are the only two headers that matter for decompressing gzip, and those are the same on both servers. Is there another response header that is incorrect?
The working response headers for the .css.gz file:
HTTP/1.1 200 OK
Cache-Control: public, max-age=604800, must-revalidate
Accept-Ranges: bytes
Content-Type: text/css
Age: 353722
Date: Tue, 07 Apr 2015 21:44:23 GMT
Last-Modified: Tue, 29 Oct 2013 17:44:18 GMT
Expires: Fri, 10 Apr 2015 19:29:01 GMT
Content-Length: 33130
Connection: keep-alive
The response headers for the .css.gz file that don't seem to work:
HTTP/1.1 200 OK
Date: Wed, 08 Apr 2015 15:14:11 GMT
Content-Type: text/css
Last-Modified: Tue, 07 Apr 2015 22:42:25 GMT
Transfer-Encoding: chunked
Connection: close
Content-Encoding: gzip
I try to write a php script to automatically download some file from a website, i use get_file_content all the response haedaers from that site, but i dont know to to save it as a file. The response header as shown as the screen image. if i access that url in browser the file will save into my computer but i cant use php script to save it.
is that possible to do it with script?
header image
thanks for helping me. i did not describe my problem very well, i want to save the attachment in that hearder, like in my header example Content-Disposition: attachment; filename=savedrecs.txt, i want to save that file into my computer
By making use of $http_response_header as the content and file_put_contents as the function for writing.
<?php
file_get_contents('http://www.stackoverflow.com'); //<--- Pass your website here
file_put_contents('test.txt',implode(PHP_EOL,$http_response_header)); //<--- Passing the $http_response_header as the text
OUTPUT :
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: http://stackoverflow.com/
Date: Wed, 26 Feb 2014 09:25:37 GMT
Connection: close
Content-Length: 148
HTTP/1.1 200 OK
Cache-Control: public, max-age=27
Content-Type: text/html; charset=utf-8
Expires: Wed, 26 Feb 2014 09:26:05 GMT
Last-Modified: Wed, 26 Feb 2014 09:25:05 GMT
Vary: *
X-Frame-Options: SAMEORIGIN
Date: Wed, 26 Feb 2014 09:25:38 GMT
Connection: close
Content-Length: 212557
I follow this way compression 1
and
compression 2
how to compression with mod_deflate? I've followed the above link and success to compress but browser can not read compression?
I get an error when I open the css and js files in a browser!
SyntaxError: illegal character
��{{�F�7���"ƫ
and
KÊO©¬NJLÎN/Ê/ÍKÑMÎÏÉ/²RN2H6II�ó>å¿rŸ—Ï9?ýSúg½/úž;RSŸ‘‡™§çZV†¤ü”Êê¤Ääìô¢üÒ¼Ýäüœü"+å$ƒd“”ÔZ�
Response Headers
Connection Keep-Alive
Content-Encoding gzip
Content-Length 127
Content-Type text/css
Date Wed, 18 Dec 2013 07:15:22 GMT
Expires Fri, 17 Jan 2014 14:15:22 +0700
Keep-Alive timeout=5, max=100
Last-Modified Wed, 18 Dec 2013 13:17:18 +0700
Server Apache/2.2.25 (Win32) PHP/5.3.26
Vary Accept-Encoding
X-Powered-By PHP/5.3.26
anybody please...thanks
It looks like you are compressing the code twice, once with PHP and once with mod_deflate.
Could this be the case?
I am using a .htaccess file to compress my .php , .css and .js files. What I want to do is to echo out in my footer is GZIP enabled or not.
For example if its not enabled by the server to echo out GZIP is not enabled or of it is to echo out GZIP is enabled.
I've looked around the net and couldn't find a php script doing that. I thought I could make it work with ini_get() but obviously unsuccessfully.
Can someone give me a tip?
Thanks :)
I have problems to understand your question. If you want to find out if the gzip encoding of your server and php configuration works, you need to check that with an additional program sending requests to the URLs in question and check if gzip is used. One such tool is curl on the commandline:
$ curl --compress --raw -i URL
Example:
$ curl --compress -I https://stackoverflow.com/questions/8029168/show-is-my-gzip-enabled-on-my-webpage
HTTP/1.1 200 OK
Cache-Control: public, max-age=18
Content-Length: 8683
Content-Type: text/html; charset=utf-8
*****************************
* Content-Encoding: deflate *
*****************************
Expires: Sun, 06 Nov 2011 18:21:05 GMT
Last-Modified: Sun, 06 Nov 2011 18:20:05 GMT
Vary: *
Date: Sun, 06 Nov 2011 18:20:46 GMT
See the line I highlighted:
Content-Encoding: deflate
it signals that something gzip is active. See RFC2616 - HTTP/1.1 14.11 Content-Encoding for the full meaning of this line.
More information is available in a related question/answer: PHP Output buffering, Content Encoding Error caused by ob_gzhandler?.
If that's not your issue maybe Gzip a website with inline PHP code is helpful.