The information in this thread might be outdated
If the solution did not help, please, check our Help Portal or contact Ecwid Team.
 
 
Thread Tools Display Modes
  #1  
Old 11-17-2015, 05:41 PM
Parsic Italia's Avatar
Parsic Italia is offline
 
Join Date: Oct 2015
Posts: 4
Default

[REST API + PHP] Troubles in updating products info


I'm trying to write a PHP script that reads a CSV file containing my products info and updates the corresponding products in my store. In particular, CSV files contain the wholesale discounts of each product. I've managed to convert that info in the right format for ECWID API, but even if Postman returns a 200 OK when I test it, running my PHP script results in a "failed to open stream: HTTP request failed!" error.

Here is my code snippet:

Code:
$dataToPut = $dataRAW;
$dataRAW = http_build_query($dataRAW);
$context = [
            'http' => [
                       'method' => 'PUT',
                       'header' =>  "Authorization: apikeystring\r\n" . "Content-Length: ".sizeof($dataToPut)."\r\n" . "Content-Type: application/json\r\n",
                       'content' => $dataToPut
                      ]                             
           ];
$context = stream_context_create($context);
$url = "https://app.ecwid.com/api/v3/".urlencode($storeId)."/products/".urlencode($productId)."?token=".$myToken;   
$dataToPut = json_encode($dataToPut);
$result = file_get_contents($url, false, $context);
If I run print_r($dataToPut); just before file_get_contents(), I obtain

Quote:
string '{"id":56782231,"wholesalePrices":[{"quantity":5,"price":5.64},{"quantity":25,"price" :5.28},{"quantity":100,"price":4.5}]}' (length=121)
It seems all ok to me... does someone know where I am wrong?
  #2  
Old 11-24-2015, 07:53 PM
Cyril D's Avatar
Cyril D Cyril D is offline
Ecwid Team
 
Join Date: Feb 2015
Location: Ecwid HQ
Posts: 530
Default

Hello there,

Thanks for your question.

I'm not sure what's wrong with your PHP code, but I recommend you looking at a PHP wrapper for the API? Maybe you'll find a method you want that works correctly.

Let me know if you have any further questions.
__________________
Cyril D.
Ecwid Customer Care Team

More tips and hints on Ecwid use in our Help Center

⬇ Please click Thanks if my reply helped you.
  #3  
Old 11-25-2015, 10:34 AM
Parsic Italia's Avatar
Parsic Italia Parsic Italia is offline
 
Join Date: Oct 2015
Posts: 4
Default

Thanks Cyril.
Sorry but I forgot to update this thread: your colleague Vitaly already resolved my problem via email: he rewrote my code by using cURL instead of file_get_contents() to connect to the API, and now my script works.
The Following User Says Thank You to Parsic Italia For This Useful Post:
Cyril D (11-25-2015)
  #4  
Old 01-29-2016, 05:10 PM
FV Hard Merchandise's Avatar
FV Hard Merchandise FV Hard Merchandise is offline
 
Join Date: Feb 2012
Posts: 10
Default

Could you post the curl code you used to update your product via api. I am having an issue getting mine to work using curl.
  #5  
Old 01-31-2016, 03:18 PM
Parsic Italia's Avatar
Parsic Italia Parsic Italia is offline
 
Join Date: Oct 2015
Posts: 4
Default

Hi Ryan and Cyril,
here's my code for the REST call:

Quote:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: app.ecwid.com','Content-Type: application/json;charset=utf-8','Cache-Control: no-cache'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataToPut);

// Make the REST call, returning the result
$response = curl_exec($curl);
echo $response;
if (!$response) {
echo("Connection Failure: ".curl_error($curl));
die();
}
curl_close($curl);
The Following User Says Thank You to Parsic Italia For This Useful Post:
Cyril D (01-31-2016)
 
The information in this thread might be outdated
If the solution did not help, please, check our Help Portal or contact Ecwid Team.

Tags
file_get_contents, http put, http request failed, php, rest

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:36 PM.
Powered by vBulletin® Version 3.8.11. Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.