Advertisement
thimo

Untitled

Mar 20th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.88 KB | None | 0 0
  1.   <?php
  2.  
  3.   $shopUrl = "https://api.spreadshirt.net/api/v1/shops/100070016/";
  4.   $user_agent = "TshirtFamilyBasket/1.0 (http://www.tshirtfamily.fr; mail@test.fr)";
  5.  
  6.     @ini_set ('user_agent', $user_agent);
  7.  
  8.   $header = array();
  9.   $header[] = createSprdAuthHeader("GET", $shopUrl);
  10.   $header[] = "Content-Type: application/xml";
  11.  
  12.   $ch = curl_init($shopUrl);
  13.   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  14.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  15.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16.   curl_setopt($ch, CURLOPT_HEADER, false);
  17.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  18.   $result = curl_exec($ch);
  19.  
  20.   // Close the handle
  21.   curl_close($ch);
  22.  
  23.   $shop = new SimpleXMLElement($result);
  24.   $namespaces = $shop->getNamespaces(true);
  25.  
  26.  
  27.  
  28.   // 5. Crée un panier
  29.   $basket = new SimpleXMLElement(getFileData("basket.xml")); // crée un panier 'temps'
  30.  
  31.   $basketsUrl = 'https://api.spreadshirt.net/api/v1/baskets';
  32.  
  33.   $header = array();
  34.   $header[] = createSprdAuthHeader("POST", $basketsUrl);
  35.   $header[] = "Content-Type: application/xml";
  36.  
  37.   $ch = curl_init($basketsUrl);
  38.   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  39.   curl_setopt($ch, CURLOPT_POST, true);
  40.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  41.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  42.   curl_setopt($ch, CURLOPT_POSTFIELDS, $basket->asXML());
  43.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44.   curl_setopt($ch, CURLOPT_HEADER, true);
  45.   $result = curl_exec($ch);
  46.   // Close the handle
  47.   curl_close($ch);
  48.  
  49.   $basketUrl = parseHttpHeaders($result, "Location");
  50.  
  51.   // 6. Crée un item dans le panier
  52.  
  53.   // je remplacerais ce num par le panier ensuite
  54.   $idBasketInterne = str_replace (' ', '', microtime());
  55.   $idBasketInterne = str_replace ('.', '', $idBasketInterne);
  56.  
  57.   $saveDataBasket = [];
  58.  
  59.   foreach ($_SESSION['articles'] as $key => $value) {
  60.  
  61.  
  62.   // enregistre les articles en bdd
  63.     $saveDataBasket[] = [
  64.           'produitId'    => $value['product']['id'],
  65.           'produitUrl'   => $value['product']['href'],
  66.           'appearance'    => $value['appearance']['name'],
  67.           'view'          => $value['view']['name'],
  68.           'productType'   => $value['productType']['name'],
  69.           'size'          => $value['size']['name'],
  70.           'quantity'      => $value['quantity'],
  71.           'basketsUrl'   => $idBasketInterne
  72.       ];
  73.  
  74.  
  75.     $articleUrl = $value['product']['href'];
  76.     $quantity = $value['quantity'];
  77.     $appearanceId = $value['appearance']['id'];
  78.     $sizeId = $value['size']['id'];
  79.  
  80.     $basketItem = new SimpleXMLElement(getFileData("basketitem.xml")); // crée un article 'temps'
  81.  
  82.  
  83.  
  84.     $itemAttributes = $basketItem->element->attributes($namespaces['xlink']);
  85.     $itemAttributes->href = $articleUrl;
  86.     $basketItem->quantity = $quantity;
  87.     $basketItem->element->properties->property[0] = $appearanceId;
  88.     $basketItem->element->properties->property[1] = $sizeId;
  89.  
  90.     $basketItemsUrl = $basketUrl."/items";
  91.  
  92.     $header = array();
  93.     $header[] = createSprdAuthHeader("POST", $basketItemsUrl);
  94.     $header[] = "Content-Type: application/xml";
  95.  
  96.     $ch = curl_init($basketItemsUrl);
  97.     curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  98.     curl_setopt($ch, CURLOPT_POST, true);
  99.     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  100.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  101.     curl_setopt($ch, CURLOPT_POSTFIELDS, $basketItem->asXML()); // envoi l'article
  102.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  103.     curl_setopt($ch, CURLOPT_HEADER, true);
  104.     $result = curl_exec($ch);
  105.  
  106.     print_r($result);
  107.  
  108.  
  109.  
  110.     // Close the handle
  111.     curl_close($ch);
  112.  
  113.   }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.   // 7. Obtenir une URL de caisse
  120.   $basketCheckoutUrl = $basketUrl."/checkout";
  121.  
  122.   $header = array();
  123.   $header[] = createSprdAuthHeader("GET", $basketCheckoutUrl);
  124.   $header[] = "Content-Type: application/xml";
  125.  
  126.   $ch = curl_init($basketCheckoutUrl);
  127. curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  128.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  129.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  130.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  131.   curl_setopt($ch, CURLOPT_HEADER, false);
  132.   $result = curl_exec($ch);
  133.   // Close the handle
  134.   curl_close($ch);
  135.  
  136.   $checkoutRef = new SimpleXMLElement($result);
  137.   $refAttributes = $checkoutRef->attributes($namespaces['xlink']);
  138.   $checkoutUrl = $refAttributes->href;
  139.  
  140.   $checkoutUrl = str_replace('en', 'fr', $checkoutUrl);
  141.   $checkoutUrl = str_replace('net', 'fr', $checkoutUrl);
  142.   $checkoutUrl = $checkoutUrl.'&shopId=100070016';
  143.  
  144.  
  145.   var_dump($checkoutUrl);
  146.  
  147.  
  148.   //je supprime le panier avant la redirection
  149.   unset( $_SESSION['articles']);
  150.   // j'enregistre le panier dans la BDD
  151.   foreach ($saveDataBasket as $saveData) {
  152.     $saveData['basketsUrl'] = $checkoutUrl;
  153.     addInDb($saveData, DB_BASKET);
  154.   }
  155.  
  156.   //redirection vers le payement
  157.   header("location:".$checkoutUrl);
  158.   die();
  159.  
  160.  
  161.  
  162. ////////////////////////////////////////////
  163. /////////// function
  164. ////////////////////////////////////////////
  165.  
  166. function createSprdAuthHeader($method, $url) {
  167.     $apiKey = "my-api-key";
  168.     $secret = "my-secret-key";
  169.     $time = time()*1000;
  170.  
  171.     $data = "$method $url $time";
  172.     $sig = sha1("$data $secret");
  173.  
  174.     return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
  175. }
  176.  
  177. function parseHttpHeaders( $header, $headername ) {
  178.     $retVal = array();
  179.     $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
  180.     foreach( $fields as $field ) {
  181.         if( preg_match('/('.$headername.'): (.+)/m', $field, $match) ) {
  182.             return $match[2];
  183.         }
  184.     }
  185.     return $retVal;
  186. }
  187.  
  188. function getFileData($file) {
  189.     $fp = fopen($file, "r");
  190.     $data = "";
  191.     while(!feof($fp)) {
  192.         $data .= fgets($fp, 1024);
  193.     }
  194.     fclose($fp);
  195.     return $data;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement