0

I followed up the solution on this post:

https://stackoverflow.com/a/4247082/1235655

I'm getting the following error:

{"success":false,"code":3003,"version":1,"message":"File not found","data":null}

What am I missing ?

Here is the code:

$boundary = 'DE(io5Kd5)NB@DQ22CeCfC4p';

define('MULTIPART_BOUNDARY', $boundary);

$header = array('Authorization' => 'Bearer ' . get_id_token(),'Content-Type' => 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY);

define('FORM_FIELD', 'file');

$filename = "generated(4).json";
$file_contents = file_get_contents($filename);

$content =  "--".MULTIPART_BOUNDARY."\r\n".
            "Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n\n".
            $file_contents."\r\n";

$content .= "--".MULTIPART_BOUNDARY."--\r\n";

$context = stream_context_create(array(
    'http' => array(
          'method' => 'POST',
          'header' => $header,
          'content' => $content,
    )
));

echo file_get_contents('https://mpop-sit.example.com/product/api/products/import', false, $context);

This is how $content looks like:

--DE(io5Kd5)NB@DQ22CeCfC4p
Content-Disposition: form-data; name="file"; filename="generated(4).json"

[
   {
    "categoryId": 80077027,
    "merchant": "b0bc3022-f76a-48ca-9a60-07b33efb90e9",
    "attributes": {
      "merchantSku": "CODE-SKU-INT-1",
      "VaryantGroupID": "Hepsiburada2",
      "Barcode": "8681080599169s",
      "UrunAdi": "Roth Tyler",
      "UrunAciklamasi": "Duis enim duis magna ex veniam elit id Lorem cillum minim nisi id aliquip. Laboris magna id est et deserunt adipisicing tempor eu ea officia ipsum deserunt. Irure occaecat sit aliquip elit ipsum sint dolore quis est amet aute pariatur cupidatat fugiat. Cillum pariatur pariatur occaecat sint. Aliqua qui in exercitation nulla aliquip id ipsum aliquip ad ut excepteur culpa consequat aliquip. Nisi ut ex tempor enim adipisicing anim irure pariatur.\r\n",
      "Marka": "Tisort.ist",
      "tax_vat_rate": "8",
      "GarantiSuresi": 24,
       "kg": "1",
      "Image1": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image2": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image3": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image4": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image5": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "kesim_kalibi": "Regular Fit",
      "kullanici_cinsiyeti": "Erkek",
      "malzeme_": "Pamuklu",
      "yaka_stili": "Bisiklet Yaka",
      "renk_variant_property": "Kırmızı",
      "beden_variant_property": "XS",
      "adet_variant_property": 99999
    }
  },
   {
    "categoryId": 80077027,
    "merchant": "b0bc3022-f76a-48ca-9a60-07b33efb90e9",
    "attributes": {
      "merchantSku": "CODE-SKU-INT-2",
      "VaryantGroupID": "Hepsiburada2",
      "Barcode": "8681080599121s",
      "UrunAdi": "Roth Tyler",
      "UrunAciklamasi": "Duis enim duis magna ex veniam elit id Lorem cillum minim nisi id aliquip. Laboris magna id est et deserunt adipisicing tempor eu ea officia ipsum deserunt. Irure occaecat sit aliquip elit ipsum sint dolore quis est amet aute pariatur cupidatat fugiat. Cillum pariatur pariatur occaecat sint. Aliqua qui in exercitation nulla aliquip id ipsum aliquip ad ut excepteur culpa consequat aliquip. Nisi ut ex tempor enim adipisicing anim irure pariatur.\r\n",
      "Marka": "Tisort.ist",
      "tax_vat_rate": "8",
      "GarantiSuresi": 24,
       "kg": "1",
      "Image1": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image2": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image3": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image4": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "Image5": "https://productimages.example.net/s/27/552/10194862145586.jpg",
      "kesim_kalibi": "Regular Fit",
      "kullanici_cinsiyeti": "Erkek",
      "malzeme_": "Pamuklu",
      "yaka_stili": "Bisiklet Yaka",
      "renk_variant_property": "Mavi",
      "beden_variant_property": "XL",
      "adet_variant_property": 99999
    }
  },
  {
   "categoryId": 80077027,
   "merchant": "b0bc3022-f76a-48ca-9a60-07b33efb90e9",
   "attributes": {
     "merchantSku": "CODE-SKU-INT-3",
     "VaryantGroupID": "Hepsiburada3",
     "Barcode": "8690605661571s",
     "UrunAdi": "Roth Tyler",
     "UrunAciklamasi": "Duis enim duis magna ex veniam elit id Lorem cillum minim nisi id aliquip. Laboris magna id est et deserunt adipisicing tempor eu ea officia ipsum deserunt. Irure occaecat sit aliquip elit ipsum sint dolore quis est amet aute pariatur cupidatat fugiat. Cillum pariatur pariatur occaecat sint. Aliqua qui in exercitation nulla aliquip id ipsum aliquip ad ut excepteur culpa consequat aliquip. Nisi ut ex tempor enim adipisicing anim irure pariatur.\r\n",
     "Marka": "Tisort.ist",
     "tax_vat_rate": "8",
     "GarantiSuresi": 24,
      "kg": "1",
     "Image1": "https://productimages.example.net/s/27/552/10194862145586.jpg",
     "Image2": "https://productimages.example.net/s/27/552/10194862145586.jpg",
     "Image3": "https://productimages.example.net/s/27/552/10194862145586.jpg",
     "Image4": "https://productimages.example.net/s/27/552/10194862145586.jpg",
     "Image5": "https://productimages.example.net/s/27/552/10194862145586.jpg",
     "kesim_kalibi": "Regular Fit",
     "kullanici_cinsiyeti": "Erkek",
     "malzeme_": "Pamuklu",
     "yaka_stili": "Bisiklet Yaka",
     "renk_variant_property": "Sarı",
     "beden_variant_property": "XL",
     "adet_variant_property": 99999
   }
 }
]

--DE(io5Kd5)NB@DQ22CeCfC4p--

Here is the CURL version, and it gives the same error:

$request = curl_init('https://mpop-sit.example.com/product/api/products/import');

curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer '.get_id_token()
));

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('generated(4).json')
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

// close the session
curl_close($request);
HOY
  • 1,067
  • 10
  • 42
  • 85
  • When i try to visit that link i got a message that the website dont exist. Most time curl works better/ easier when you try to get data from an other website or API – Baracuda078 Jun 11 '20 at 19:15
  • because i didn't wanted to give the name of the site, to make it work, change "example" text inside the links with "hepsiburada" – HOY Jun 11 '20 at 19:27
  • my bad, I didnt noticed the example in the url. But did you also try this with curl? some times I also have problems with file_get_contents() and when I use curl it works flawless – Baracuda078 Jun 11 '20 at 19:30
  • @Baracuda078, I added the curl version of the code, I never worked with curl before, so can you take a look ? Because the error message didn't change after I wrote the curl version. – HOY Jun 11 '20 at 23:06

1 Answers1

0

Maybe the example in this post will help you https://www.php.net/manual/en/class.curlfile.php#115569

Adding an user-agent can help, maybe the api is checking that value to make sure the requeat is valid. Set CURLOPT_SSL_VERIFYPEER to false this can help when you try to make a request to an https link

Add the file by using curl_file_create()

Its hard to answer your question because I know nothing about the API you are using.

Baracuda078
  • 677
  • 1
  • 5
  • 10