/** * @param $data * @return array * @todo create auth token automatically or change completely to client_id / client_secreted auth * and put this completely into an external config file. */ function postProductTypes($data) { $api = 'https://api.sphere.io/'; $project = ''; $endpoint = '/product-types'; $bearer = ''; // get files and encode to valid json strings $jsondata = file_get_contents($data); $url = $api . $project . $endpoint; $ch = curl_init($url); importLog('Initiated cURL Session'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($jsondata), 'Authorization: Bearer ' . $bearer)); importLog('Set Options for cURL Session'); $result = curl_exec($ch); importLog('Executed cURL Session, saved Result'); curl_close($ch); importLog('Closed cURL Session'); var_dump($result); // returns result, but is not handled so far // @todo Should handle the result in another helper method somehow, e.g. for saving created IDs. return $result; }
/** * @param $files * @return null */ function _run($files) { importLog('START Import'); foreach ($files as $file) { $file = __DIR__ . '/data/types_nested/' . $file; importLog($file); if (file_exists($file)) { postProductTypes($file); } } importLog('DONE Import'); return null; }