Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $shopUrl = "https://api.spreadshirt.net/api/v1/shops/100070016/";
- $user_agent = "TshirtFamilyBasket/1.0 (http://www.tshirtfamily.fr; mail@test.fr)";
- @ini_set ('user_agent', $user_agent);
- $header = array();
- $header[] = createSprdAuthHeader("GET", $shopUrl);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init($shopUrl);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $result = curl_exec($ch);
- // Close the handle
- curl_close($ch);
- $shop = new SimpleXMLElement($result);
- $namespaces = $shop->getNamespaces(true);
- // 5. Crée un panier
- $basket = new SimpleXMLElement(getFileData("basket.xml")); // crée un panier 'temps'
- $basketsUrl = 'https://api.spreadshirt.net/api/v1/baskets';
- $header = array();
- $header[] = createSprdAuthHeader("POST", $basketsUrl);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init($basketsUrl);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $basket->asXML());
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- $result = curl_exec($ch);
- // Close the handle
- curl_close($ch);
- $basketUrl = parseHttpHeaders($result, "Location");
- // 6. Crée un item dans le panier
- // je remplacerais ce num par le panier ensuite
- $idBasketInterne = str_replace (' ', '', microtime());
- $idBasketInterne = str_replace ('.', '', $idBasketInterne);
- $saveDataBasket = [];
- foreach ($_SESSION['articles'] as $key => $value) {
- // enregistre les articles en bdd
- $saveDataBasket[] = [
- 'produitId' => $value['product']['id'],
- 'produitUrl' => $value['product']['href'],
- 'appearance' => $value['appearance']['name'],
- 'view' => $value['view']['name'],
- 'productType' => $value['productType']['name'],
- 'size' => $value['size']['name'],
- 'quantity' => $value['quantity'],
- 'basketsUrl' => $idBasketInterne
- ];
- $articleUrl = $value['product']['href'];
- $quantity = $value['quantity'];
- $appearanceId = $value['appearance']['id'];
- $sizeId = $value['size']['id'];
- $basketItem = new SimpleXMLElement(getFileData("basketitem.xml")); // crée un article 'temps'
- $itemAttributes = $basketItem->element->attributes($namespaces['xlink']);
- $itemAttributes->href = $articleUrl;
- $basketItem->quantity = $quantity;
- $basketItem->element->properties->property[0] = $appearanceId;
- $basketItem->element->properties->property[1] = $sizeId;
- $basketItemsUrl = $basketUrl."/items";
- $header = array();
- $header[] = createSprdAuthHeader("POST", $basketItemsUrl);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init($basketItemsUrl);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $basketItem->asXML()); // envoi l'article
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- $result = curl_exec($ch);
- print_r($result);
- // Close the handle
- curl_close($ch);
- }
- // 7. Obtenir une URL de caisse
- $basketCheckoutUrl = $basketUrl."/checkout";
- $header = array();
- $header[] = createSprdAuthHeader("GET", $basketCheckoutUrl);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init($basketCheckoutUrl);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- $result = curl_exec($ch);
- // Close the handle
- curl_close($ch);
- $checkoutRef = new SimpleXMLElement($result);
- $refAttributes = $checkoutRef->attributes($namespaces['xlink']);
- $checkoutUrl = $refAttributes->href;
- $checkoutUrl = str_replace('en', 'fr', $checkoutUrl);
- $checkoutUrl = str_replace('net', 'fr', $checkoutUrl);
- $checkoutUrl = $checkoutUrl.'&shopId=100070016';
- var_dump($checkoutUrl);
- //je supprime le panier avant la redirection
- unset( $_SESSION['articles']);
- // j'enregistre le panier dans la BDD
- foreach ($saveDataBasket as $saveData) {
- $saveData['basketsUrl'] = $checkoutUrl;
- addInDb($saveData, DB_BASKET);
- }
- //redirection vers le payement
- header("location:".$checkoutUrl);
- die();
- ////////////////////////////////////////////
- /////////// function
- ////////////////////////////////////////////
- function createSprdAuthHeader($method, $url) {
- $apiKey = "my-api-key";
- $secret = "my-secret-key";
- $time = time()*1000;
- $data = "$method $url $time";
- $sig = sha1("$data $secret");
- return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
- }
- function parseHttpHeaders( $header, $headername ) {
- $retVal = array();
- $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
- foreach( $fields as $field ) {
- if( preg_match('/('.$headername.'): (.+)/m', $field, $match) ) {
- return $match[2];
- }
- }
- return $retVal;
- }
- function getFileData($file) {
- $fp = fopen($file, "r");
- $data = "";
- while(!feof($fp)) {
- $data .= fgets($fp, 1024);
- }
- fclose($fp);
- return $data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement