0

I'm trying to get a file/image downloaded to a folder on my first server, from second server. I have the following code:

$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg';

$imageName = pathinfo( $image, PATHINFO_BASENAME );

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $image );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

$source = curl_exec( $ch );
curl_close( $ch );

file_put_contents( './content/' . $imageName, $source );

$out = preg_split('/(\r?\n){2}/', $source, 2);
    $headers = $out[0];
    $headersArray = preg_split('/\r?\n/', $headers);
    $headersArray = array_map(function($h) {
        return preg_split('/:\s{1,}/', $h, 2);
    }, $headersArray);

    $tmp = [];
    foreach($headersArray as $h) {
        $tmp[strtolower($h[0])] = isset($h[1]) ? $h[1] : $h[0];
    }
    print_r($tmp);

The linked url is not of my servers, but works as expected, writing a file on my first server. But when I use my own second server link, for example https://example.com/demo1.png, it writes a file of 0 bytes.

On logging out the headers, the external link which is not of my server logs out an array of many items about the image such as "content-type" and "content-length". On logging out the header response of the image of my second server, it logs out an empty array..

What adjustments do I need to do on the script? I'm also okay to do some adjustments on my second server(?), if needed. Thank you in advance.

Flora42
  • 21
  • 5
  • 2
    We can not possibly know why your server would not want to respond properly to that particular request. Have you checked what errors cURL might have to report? Have you checked the access and error log on your server yet? – CBroe Oct 18 '22 at 11:24
  • `What adjustments do I need to do`...that depends on what the actual error is, which you haven't told us. Have you inspected the response headers from curl? Any errors logs on the remote webserver itself? Anything like that? – ADyson Oct 18 '22 at 11:25
  • It is really NOT CLEAR what you are suggestion that you are doing. The code you show us WORKS, so possibly we need to see the code that DOES NOT WORK and a bit more clear explanation of your issue – RiggsFolly Oct 18 '22 at 11:27
  • to improve your experience on SO please [take the tour](http://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask), an [On Topic question](https://stackoverflow.com/help/on-topic), then look at the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist), the [perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Oct 18 '22 at 11:28
  • I understand, please forgive. I have edited the question to better show the issue. I will also remember to be more specific of any errors when posting on SO from now. – Flora42 Oct 18 '22 at 11:41
  • @RiggsFolly I made use of a piece of code from SO as I never had experience logging out header responses, further edited my post with it. I would have also posted header response log from the external link which is of not my server but that seemed not necessary. – Flora42 Oct 18 '22 at 11:50
  • What exactly are you expecting from this code. When I run it i get a lot of nonsense in an array which of course is expected as the file you are trying to mess with is an image and why you would want to make an array of a image I cannot tell – RiggsFolly Oct 18 '22 at 11:57
  • _But when I use my own second server link_ I assume this mens you change `$image = ...` to point to an image file on another of your servers and attempt to fetch that from that server like you did from the `http://i1.au.reastatic.net` server, am I right – RiggsFolly Oct 18 '22 at 12:01
  • @RiggsFolly Its quite awkward for me to not be able to provide with better information, as I don't know how to get that. Regarding the image, when I use my server's image link, it returns an empty array as it is not able to access the image on the server itself. – Flora42 Oct 18 '22 at 12:04
  • @RiggsFolly yes, you are right on that comment. – Flora42 Oct 18 '22 at 12:05
  • Well first I would check for curl errors, see this answer for how to do that https://stackoverflow.com/a/3987037/2310830 – RiggsFolly Oct 18 '22 at 12:09
  • @RiggsFolly Thank you for the help, I understand how to retrieve curl errors now. And about the case, I'm receiving a "SSL certificate problem: unable to get local issuer certificate" error. My second server has actual SSL on its sites, while this first "server" that I'm trying to download image/files into is a local one. I'm using a software called LocalWP which installs WordPress locally, and also has an inbuilt router than provides a domain address for it like a cover for "localhost", and also provides an inbuilt SSL, if I word it that way. I am indeed on a https address of my local server – Flora42 Oct 18 '22 at 12:19
  • I thought that might be it. You need to download an configure a newer cacert.pem from https://curl.se/docs/caextract.html and put it somewhere and amend your PHP.INI to point to this new file something like this `curl.cainfo="path to the/cacert.pem"` – RiggsFolly Oct 18 '22 at 12:28
  • @RiggsFolly Thank you. I have put the cacert.pem file in the root , and I tried with `curl.cainfo="./cacert.pem"` , `curl.cainfo="cacert.pem"` , but I think I might be putting it wrong. To be more specific, the folder which I need to put the image file into, I can write it as `./content/`, hence it seemed right to do `./cacert.pem` considering as the root or the base where its located, which hasn't worked – Flora42 Oct 18 '22 at 13:50
  • I would place it in a folder that the Server has access to. I would hope it cannot access the root folder, but I am not a linux user. I place mine in the PHP folder if that helps but I am a windows user – RiggsFolly Oct 18 '22 at 13:53
  • @RiggsFolly When I visit `example.com/cacert.pem`, the file starts to download. I believe it is in a readable place. And hope it is alright to have it there. Its not necessarily the root of server maybe, but root folder of the domain which I felt right and more accessible to go with – Flora42 Oct 18 '22 at 13:56
  • Oh I see, ok. There are question and answers on here related to that error, you shoudl search for them and see what others did to this – RiggsFolly Oct 18 '22 at 13:59
  • ALso you may need to add another curl option for https sites – RiggsFolly Oct 18 '22 at 13:59

1 Answers1

0

Thanks to @RiggsFolly , I found out the issue was related to SSL certificate. The issue seems to be on my local pc server, and may not persist on a live server. I will move to a live server soon, and if the issue persists I will update this answer as needed. I'll mark this answer as best answer, hoping to also let others know that the issue was related with SSL and can be further fixed with proper measures. SO also has many useful questions/answers for the mentioned SSL issue.

Flora42
  • 21
  • 5
  • But it's not an HTTPS request. Why would SSL be in the picture?? What do you mean there is no header. This is very simple. It can easily be done file_get_contents(). The response header: HTTP/1.1 200 OK Content-Type: image/jpeg Content-Length: 7415 Connection: keep-alive Cache-Control: max-age=2419200, s-maxage=31536000 Date: Tue, 18 Oct 2022 11:19:36 GMT Last-Modified: Thu, 05 Jul 2012 09:34:39 GMT Server: nginx X-original-format: jpeg X-original-height: 600 X-original-width: 800 X-resized-format: jpeg X-resized-height: 112 X-resized-width: 150 X-Cache: Hit from cloudfront – Misunderstood Oct 20 '22 at 06:54