0

I have been creating a script to insert data from a JSON file to a POST request.

While it does what it needs to download the image in the URL supplied than it needs to download it and perform a post request on my route.

I have been struggling for the past few hours to get it working but no success.

I have used realpath and CurlFile but it didn't make it work.

Here is the request:

<?php
$image = $this->makeCurlFile(realpath("scorpion_stretch-59788999e4432d80.jpg"));

$body = ["name" => "uploaded_file", "filename" => $image->name];

echo print_r($body);

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://resources.diamondappgroup.com/upload.php",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => array(
        "Accept: */*",
        "Cache-Control: no-cache",
        "Connection: keep-alive",
        "Content-Type: application/x-www-form-urlencoded",
        "Host: resources.diamondappgroup.com",
        "Postman-Token: 45ebd192-9621-482c-8735-2061bc31c6fa,6a7a959f-372d-4afb-9efa-6b4da47ea1b0",
        "User-Agent: PostmanRuntime/7.11.0",
        "accept-encoding: gzip, deflate",
        "cache-control: no-cache",
        "content-length: 312659",
        "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}
Rahul
  • 18,271
  • 7
  • 41
  • 60
DaAmidza
  • 336
  • 2
  • 7
  • 25
  • Possible duplicate of [Php upload image to remote server with cURL](https://stackoverflow.com/questions/35776859/php-upload-image-to-remote-server-with-curl) – stefo91 May 30 '19 at 10:14
  • This looks like [a simple and clear tutorial](http://code.iamkate.com/php/sending-files-using-curl). I don't see the @ anywhere in your code. The [manual](https://www.php.net/manual/en/function.curl-setopt.php) states: "To post a file, prepend a filename with @ and use the full path." – KIKO Software May 30 '19 at 10:14

0 Answers0