1

For a project I need to fetch the file size of a GitHub release file using CURL without actually downloading the entire file.

$url = 'https://github.com/atom/atom/releases/download/v1.10.2/AtomSetup.exe'

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

curl_close($ch);

echo $size;
echo $data;

As I have figured out CURLOPT_NOBODY allows me to just get the size of the file (from it's header) without actually downloading the entire file. If I remove line I do get the filesize but that also downloads the entire file, and that's something I'm trying to avoid.

CURLOPT_NOBODY also replaces "GET" request with "HEAD" and I guess that's the part of the problem.

I have also tried setting up CURLOPT_USERAGENT, CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR but that didn't work out.

Any help is warm welcome!

Here is the complete CURL output:

HTTP/1.1 302 Found
Server: GitHub.com
Date: Mon, 06 Feb 2017 16:19:08 GMT
Content-Type: text/html; charset=utf-8
Status: 302 Found
Cache-Control: no-cache
Vary: X-PJAX
Location: https://github-cloud.s3.amazonaws.com/releases/3228505/5b5e9204-7507-11e6-8019-f5a3bc356747.exe?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA ▶
X-UA-Compatible: IE=Edge,chrome=1
Set-Cookie: _gh_sess=eyJzZXNzaW9uX2lkIjoiNWFjZWNhZDEzMjFmMmFkNmFjOGMxOTIwNzRlNGQzNTEiLCJzcHlfcmVwbyI6ImF0b20vYXRvbSIsInNweV9yZXBvX2F0IjoxNDg2Mzk3OTQ4fQ%3D%3D--2 ▶
X-Request-Id: 4d192195dc74c4f6a056710d3fd30be9
X-Runtime: 0.120950
Content-Security-Policy: default-src 'none'; base-uri 'self'; connect-src 'self' uploads.github.com status.github.com collector.githubapp.com api.github.com www ▶
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Public-Key-Pins: max-age=5184000; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho="; pin-sha2 ▶
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding
X-Served-By: 97b025644def4c59b05efb255b209cda
X-GitHub-Request-Id: CF4C:3E44:58EC2B1:8D98E31:5898A1FB

HTTP/1.1 403 Forbidden
x-amz-request-id: 962EAB609235815D
x-amz-id-2: 1uccfAjq9MwgFo5BUGLhFkCjCNhkQKpVnAU9s6AF0pNy0fPqnEw6Se+UOa1RESRxyAmtoZXKImw=
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Mon, 06 Feb 2017 16:19:07 GMT
Server: AmazonS3

closed as off-topic by Simon Hayter Feb 6 '17 at 17:48

  • This question does not appear to be about webmastering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Sorry but this question is off-topic because it is related to PHP programming which is best suited on Stack Overflow. Normally we would migrate such questions across but this particular question is likely duplicate. E.G stackoverflow.com/questions/2602612/… – Simon Hayter Feb 6 '17 at 17:48

Browse other questions tagged or ask your own question.