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
Related
I am attempting to add a custom header within a Laravel(8) response called X-Custom which works, but it places it at the top of the header stack as such:
HTTP/1.1 200 OK
Date: Mon, 01 Nov 2021 10:58:44 GMT
Server: Apache/2.4.29 (Ubuntu)
X-Custom: 8675309
Cache-Control: no-cache, private
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Access-Control-Allow-Origin: *
Content-Length: 15
Content-Type: text/html; charset=UTF-8
Content is here
With low memory devices (an ATTiny85) the earliest header I manage to receive is X-RateLimit-Remaining: 59. So to get the X-Custom header I ideally need it to go on the bottom of the whole stack like so:
HTTP/1.1 200 OK
Date: Mon, 01 Nov 2021 10:58:44 GMT
Server: Apache/2.4.29 (Ubuntu)
Cache-Control: no-cache, private
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Access-Control-Allow-Origin: *
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Custom: 8675309
Content is here
From what I can see Laravel returns the various headers from different files rather than all in one go so I think a simple sort is out of the question.
Is sticking a header on the end achieveable within Laravel's means?
Edit: On Chris Haas's comment it made me realise I don't need the headers as I'm only looking for one. Middleware was a dead end as it all seems to run just prior to all these Laravel headers being pushed.
I have discovered if I comment out $this->sendHeaders() within symfony/http-foundation/Response.php::send() , it clears it up to this:
HTTP/1.1 200 OK
Date: Tue, 02 Nov 2021 01:21:29 GMT
Server: Apache/2.4.29 (Ubuntu)
X-Custom: 8675309
Content-Length: 15
Content-Type: text/html; charset=UTF-8
Content is here
I think this is as clean as I can get it from Laravel. This may be enough to get these devices working but in the effort to properly answer this question I am going to see what I can do from the Apache side.
Edit2: Following this answer https://stackoverflow.com/a/24941636/3417896 I can remove the Content-Type header by setting header("Content-type:") at the same place I'm commenting out sendHeaders(), resulting in this:
HTTP/1.1 200 OK
Date: Tue, 02 Nov 2021 02:23:45 GMT
Server: Apache/2.4.29 (Ubuntu)
X-Custom: 8675309
Content-Length: 15
Content is here
Ok now the Content-length I think remains in Apache. I saw another answer on how to remove it from php here https://stackoverflow.com/a/31563538/3417896, but it adds another header in its place.
So I've got a server to server application. The PHP script on server 1, domain 1 sets a custom header in the page (Authorization: Bearer 123456789). The script on server 2, domain 2 uses get_headers() to read the headers.
It all works fine when the files are served natively. But when the script on server 1 is included in a Joomla module get_headers() doesn't retrieve the custom header.
In both cases, developer tools shows the custom header but also some different headers than returned by get_headers().
The code below uses JFactory to set the headers if Joomla is loaded but it is the same result using header(). Joomla just isn't passing the custom header.
I don't get it. Anyone have any idea what is going on here? Its not a SEF or htaccess issue.
<?php
// Server 1
if(!class_exists("JFactory")){ // no Joomla
header('Authorization: Bearer 123456789');
} else { // Joomla framework loaded
$app = JFactory::getApplication();
$app->setHeader('Authorization: ', 'Bearer 123456789');
$app->sendHeaders();
}
The code on server 2:
<?php
// Server 2
$headers = get_headers("http://server1.com/");
foreach($headers as $header) {
echo $header ."<br/>";
}
Output from get_headers() when served natively:
HTTP/1.1 200 OK
Date: Thu, 19 Jan 2017 12:44:35 GMT
Server: Apache
Authorization: Bearer 123456789
Content-Length: 0
Connection: close
Content-Type: text/html
Output from get_headers() when served by Joomla:
HTTP/1.1 200 OK
Date: Thu, 19 Jan 2017 12:45:49 GMT
Server: Apache
Set-Cookie: 3c460b3da9ecb202e794816b4144c6ff=ja7mn4b4njov98lsv76kk8pvu2; path=/; HttpOnly
Vary: Accept-Encoding
Content-Length: 1264
Connection: close
Content-Type: text/html
Native headers displayed by developer tools:
Authorization: Bearer 123456789
Date: Thu, 19 Jan 2017 13:07:32 GMT
Server: Apache
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 0
Content-Type: text/html
200 OK
Joomla headers displayed by developer tools:
Pragma: no-cache
Date: Thu, 19 Jan 2017 12:19:24 GMT
Last-Modified: Thu, 19 Jan 2017 12:19:25 GMT
Server: Apache
Authorization: : Bearer 123456789
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 76888
Expires: Wed, 17 Aug 2005 00:00:00 GMT
Remove double dot from setheader call :
$app = JFactory::getApplication();
$app->setHeader('Authorization', 'Bearer 123456789');
$app->sendHeaders();
Thanks for the suggestion Yoleth. I tested this and got the same result.
However I have found the problem. The Joomla site setting the header is using a component called Site Lock. This is similar to putting the site off line but has some nice features for developers.
Basically Site Lock was preventing the page being served and just returning the headers from the lock page (as it should). I don't know why I didn't see it earlier. Sometimes just can't see the forest for the trees!
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
calling a .html on my website directly the header will be:
HTTP/1.1 200 OK
Date: Tue, 07 May 2013 14:53:30 GMT
Server: Apache
Last-Modified: Tue, 24 Aug 2012 21:51:42 GMT
ETag: "1431a086-1e01-78e98c5498f1c"
Accept-Ranges: bytes
Content-Length: 7681
Vary: Accept-Encoding
Content-Type: text/html
now the request is forwarded through a php script like
(- the use of the php script here is only to filter some words from the html before delivering it by a regex and to add a footer to every page)
and the header looks like:
HTTP/1.1 200 OK
Date: Tue, 07 May 2013 14:52:50 GMT
Server: Apache
Vary: User-Agent,Accept-Encoding
Content-Type: text/html
Question: How to keep "Last-Modified: ..." and "ETag: ..." ?
Thanks=)
I am using wget in php script to download images from the url submitted by the user. Is there some way for me to determine the size of image before actually downloading it and restricting the size of download to 1mb? Also can I possibly check that the url points to an image only and not an entire website without downloading?
I dont want to end up filling my server with malware.
Before loading you can check headers (you'll have to download them though). I use curl - not wget. Here's an example:
$ curl --head http://img.yandex.net/i/www/logo.png
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 16 Jun 2012 09:46:36 GMT
Content-Type: image/png
Content-Length: 3729
Last-Modified: Mon, 26 Apr 2010 08:00:35 GMT
Connection: keep-alive
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Accept-Ranges: bytes
Content-Type and Content-Length should normally indicate that the image is ok