skip to Main Content

This is the file I want to download:

https://sourceforge.net/projects/minpspw/files/SDK%20%2B%20devpak/pspsdk%200.11.2/pspsdk-setup-0.11.2r3.exe

I can download it on the command line like this (please replace <URL>):

curl --output pspsdk-setup-0.11.2r3.exe -L <URL>

There are 4 redirects but at the end it saves a 32.6 MB file. Now I try to do the same in PHP:

<?php 

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://sourceforge.net/projects/minpspw/files/SDK%20%2B%20devpak/pspsdk%200.11.2/pspsdk-setup-0.11.2r3.exe");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_MAXREDIRS, 10);
curl_setopt($c, CURLOPT_AUTOREFERER, true);
curl_setopt($c, CURLOPT_COOKIEJAR, "cookie.jar");
curl_setopt($c, CURLOPT_USERAGENT, "curl/7.74.0"); // same as command line curl

file_put_contents("pspsdk-setup-0.11.2r3.exe", curl_exec($c));
curl_close($c);

But I only get an HTML landing page saying "Your download will start shortly…" (The landing page is written to file pspsdk-setup-0.11.2r3.exe).

As you can see in PHP I only get a landing page while command line curl downloads the actual exe file.

I tried to enable the referer, cookies and I’ve also set the user agent to the same as the command line curl.

How to fix my PHP code so it downloads the exe file and not only a landing page?

2

Answers


  1. I got this working by disabling the referer header:

    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, "https://sourceforge.net/projects/minpspw/files/SDK%20%2B%20devpak/pspsdk%200.11.2/pspsdk-setup-0.11.2r3.exe");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_MAXREDIRS, 10);
    curl_setopt($c, CURLOPT_COOKIEJAR, "cookie.jar");
    curl_setopt($c, CURLOPT_USERAGENT, "curl/7.85.0");
    
    file_put_contents("pspsdk-setup-0.11.2r3.exe", curl_exec($c));
    curl_close($c);  
    
    Login or Signup to reply.
  2. You can use the following code, It directly downloads the file and store at the location where the script is stored.

    $url = "https://sourceforge.net/projects/minpspw/files/SDK%20%2B%20devpak/pspsdk%200.11.2/pspsdk-setup-0.11.2r3.exe";
    $local_file = "pspsdk-setup-0.11.2r3.exe";
    
    $fp = fopen($local_file, 'w');
    
    $c = curl_init($url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_FILE, $fp);
    
    $data = curl_exec($c);
    
    curl_close($c);
    fclose($fp)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search