function getToken($server_service,$username,$password) { /** get a REST web API token from magento (we mimic the following console curl command) curl -X POST "http://domain.com/magento/index.php/rest/V1/integration/admin/token" \ -H "Content-Type:application/json" -d '{"username":"admin", "password":"admin123"}' * * @param string $server_service - server name and service to connect to * @param string $username - authorized user * @param string $username - password for authorized user */ $endpoint = $server_service.'/V1/integration/admin/token'; $credentials = json_encode(array('username' => $username, 'password' => $password)); $ch = curl_init($endpoint); # create new cURL resource curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $credentials); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($credentials)) ); $token = curl_exec($ch); // Check for errors and return any error message $curlError = ""; # no error if(curl_errno($ch) != 0) { $errno = curl_errno($ch); $error_message = curl_strerror($errno); $curlError = "cURL error ({$errno}): {$error_message}"; } curl_close($ch); return array($curlError,$token); } function updateProduct($server_service,$sku,$updates,$headers) { /** * update a products existing attributes * reference: http://devdocs.magento.com/swagger/index.html#!/catalogProductRepositoryV1 (we mimic this curl console command) curl -X PUT "http://domain.com/magento/index.php/rest/V1/products/24-MB01" \ -d '{"product" : {"price": 12345}}' -H "Content-Type:application/json" \ -H "Authorization: Bearer omcv3y5pydj4iemrfjnrhenay6nqb3vf" * * @param string $server_service - server name and service to connect to * @param string $sku - sku of the product to be updated * @param array $updates - array of the individual columns be updated * @param array $headers - the curl header */ $endpoint = $server_service.'/V1/products/'.$sku; $productUpdates = json_encode(array('product' => $updates)); $ch = curl_init(); # create new cURL resource curl_setopt($ch, CURLOPT_URL, $endpoint); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$productUpdates); curl_exec($ch); // Check for errors and return any error message $curlError = ""; # no error if(curl_errno($ch) != 0) { $errno = curl_errno($ch); $error_message = curl_strerror($errno); $curlError = "cURL error ({$errno}): {$error_message}"; } $requestHeaders = curl_getinfo($ch)['request_header']; # just for info curl_close($ch); return array($curlError,$requestHeaders); }