Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Définition

Qu'est-ce que la méthode CURL ? 

Il s'agit d'une méthode d'intégration qui permet de réaliser la même chose qu'en Javascript mais en PHP

 

 

Où l'installer?

Cette méthode ne fonctionne que pour la création de compte et la remontée de commande

Voici les deux fichiers nécessaires

api-createorder.php

<?php header('Content-Type: text/html; charset=utf-8'); ?>

<html>

 <head>

 </head>

 <body>

<?php
/**
* User: olivier
* Date: 29/12/12
* Time: 14:55
*/

@session_start();

if (isset($_GET['kill']))

{

unset($_SESSION['publicKey']);
unset($_SESSION['privateKey']);
unset($_SESSION['order_id']);
header('Location: api-createorder.php?ok');
exit;

}

if (isset($_POST['publicKey']))

{ /* juste utilisable pour test */

$_SESSION['publicKey']=$_POST['publicKey'];
$_SESSION['privateKey']=$_POST['privateKey'];

}

if (isset($_SESSION['publicKey']) && $_SESSION['publicKey'])

{

// récupération de clé
$publicKey = $_SESSION['publicKey'];
$privateKey = $_SESSION['privateKey'];
echo "<p>Les clés d'API sont stockes en session : $publicKey / $privateKey <a href='api-createorder.php?kill'>Kill</a></p>";
if (isset($_COOKIE['sbt']))

{

echo "<p>Le cookie SBT local est : ".$_COOKIE['sbt']."</p>";

}

else

{

echo "<p>Il n'y a pas de cookie SBT, il faut recharcher la page pour que le javascript puisse transférer le cookie</p>";

}

}

else

{

echo '<h2>Entrez vos cles d\'API pour le test
<form method="post" action="">
Clé publique : <INPUT type="text" name="publicKey" value=""><br/>
clé privée : <INPUT type="text" name="privateKey" value="">
<INPUT type="submit" value="Connect">
</form>';
exit;

}


/*

if (isset($_POST['testCreationCde'])) {

// Création d'une commande de test avec le tracker Javascript
$_SESSION['order_id']=rand(1,1000);
echo '<script type="text/javascript">
var publicKeySb = "'.$publicKey.'" ;
var iproto = (("https:" == document.location.protocol) ? "https://" : "http://");';

echo' document.write(unescape("%3Cscript src=\'" + iproto + "static-sb.com/js/sb-tracker.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var siteid = "'.$publicKey.'" ;
var orderemail = "john@doe.com" ; // à remplacer
var orderamount = 1 ; //à remplacer
var ordername = "John Doe" ; //à remplacer
var idorder = "'.$_SESSION['order_id'].'" ; //à remplacer
var orderoptin = 1 ;
var orderstate = 10 ;
var ordertest = 0 ;
</script>
';
echo "<h2>Commande ".$_SESSION['order_id']." créé</h2>";

} else

*/

 

if (isset($_POST['createOrder']))

{

// Création d'une commande via API
$post_data = array();
if ($_POST['order_state']!='')

{

$post_data['data']['order_state'] = $_POST['order_state'];

}
if ($_POST['order_num']!='')

{

$post_data['data']['order_num'] = $_POST['order_num'];

}
if ($_POST['amount']!='')

{

$post_data['data']['order_amount'] = $_POST['amount'];

}
if ($_POST['order_idorder']!='') // mise à jour de l'order_idorder

{

$post_data['data']['order_idorder'] = $_POST['order_idorder'];
$_SESSION['order_id'] = $_POST['order_idorder'];

}

$post_data['data']['customer_email'] = 'omartineau@gmail.com';
$post_data['data']['customer_name'] = 'Martineau';
$post_data['data']['customer_firstname'] = 'olivier';
$post_data['data']['customer_country'] = 'fr';
$post_data['data']['customer_birthday'] = '1970-12-31';
$post_data['data']['customer_gender'] = 'male'; // female

$post_data['data']['customer_num1'] = '100';
$post_data['data']['customer_num2'] = '200';
$post_data['data']['customer_txt1'] = 'text1';
$post_data['data']['customer_txt2'] = 'text3';
$post_data['data']['customer_date1'] = '2010-10-10';
$post_data['data']['customer_date2'] = '2011-11-11';
$post_data['data']['customer_bool1'] = '0';
$post_data['data']['customer_bool2'] = '1';

$post_data['data']['action'] = 'order'; // "signup", ou par defaut "order"
$post_data['data']['customer_isoptin'] = '1';


// Très important, sinon le tracking ne peux pas se faire
// ATTENTION dans le javascript, l'option "sbsetcookie" doit être à true
// c'est ce qui permet de transféré le cookie du nom de domaine SpreadButton au domaine local.
$post_data['data']['customer_cookie'] = $_COOKIE['sbt'];

// ajout de la clé publique
$post_data['public'] = $_SESSION['publicKey'];

#ksort($post_data['data']); // par convention, on tri toujours avant de signer
if (isset($post_data['request'])) // request n'est pas utilisé dans ce cas, mais c'est mieux de le signer !

{

#ksort($post_data['request']); // par convention, on tri toujours avant de signer

}
#ksort($post_data); // par convention, on tri toujours avant de signer
$curl_post_data = http_build_query($post_data);

// Création de la signature
$sign = sha1($curl_post_data . $_SESSION['privateKey']);

echo "<p>Chaine signée : ".$curl_post_data . $_SESSION['privateKey']."<br/>Signature : ".$sign."</p>";

// finalisation des valeurs postés
$curl_post_data .= '&sign' . '='. $sign;

// Appel de l'API pour mettre à jour des informations
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://sb.am/api/CreateAction");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$curl_post_data );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_return = curl_exec($curl);
curl_close($curl);
echo "<h2>Retour appel API</h2><pre>";
echo $curl_return;
echo "</pre>";

}

// formulaire création d'une commande
echo '<h2>Création d\'une commande</h2>
<form method="post" action="">
Numéro de commande : <INPUT type="text" name="order_idorder" value=""><br/>
Numéro de facture : <INPUT type="text" name="order_num" value=""><br/>
Montant de facture : <INPUT type="text" name="amount" value=""><br/>
Etat de la commande : <select name="order_state">
<option value="10">Commande enregistrée</option>
<option value="0">Commande annulée</option>
<option value="20">Commande payée</option>
<option value="50">Commande expédiée</option>
<option value="100">Commande reçue</option>
</select> <br>
<INPUT type="submit" name="createOrder" value="Enregistrer">
</form>';

echo '<script type="text/javascript">
var publicKeySb = "'.$publicKey.'" ;
var sbsetcookie = true;
var iproto = (("https:" == document.location.protocol) ? "https://" : "http://");';

echo' document.write(unescape("%3Cscript src=\'" + iproto + "static-sb.com/js/sb-tracker.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>';
?>

</body>

</html>

api-updateorder.php

<?php header('Content-Type: text/html; charset=utf-8'); ?>

<html>

<head>
</head>
<body>

<?php
/**
* User: olivier
* Date: 29/12/12
* Time: 14:55
*/

@session_start();

if (isset($_GET['kill']))

{

unset($_SESSION['publicKey']);
unset($_SESSION['privateKey']);
unset($_SESSION['order_id']);
header('Location: api-updateorder.php?ok');
exit;

}


if (isset($_GET['publicKey'])) /* juste utilisable pour test */

$_SESSION['publicKey']=$_GET['publicKey'];
$_SESSION['privateKey']=$_GET['privateKey'];

}

 

if (isset($_SESSION['publicKey']) && $_SESSION['publicKey'])

{

// récupération de clé
$publicKey = $_SESSION['publicKey'];
$privateKey = $_SESSION['privateKey'];
echo "<p>Les clés d'API sont stockes en session : $publicKey / $privateKey <a href='api-updateorder.php?kill'>Kill</a></p>";

}

else

{

echo '<h2>Entrez vos cles d\'API pour le test
<form method=get action="">
Clé publique : <INPUT type="text" name="publicKey" value=""><br/>
clé privée : <INPUT type="text" name="privateKey" value="">
<INPUT type="submit" value="Connect">
</form>';
exit;

}


if (isset($_POST['testCreationCde']))

{

// Création d'une commande de test avec le tracker Javascript
$_SESSION['order_id']=rand(1,1000);
echo '<script type="text/javascript">
var publicKeySb = "'.$publicKey.'" ;
var iproto = (("https:" == document.location.protocol) ? "https://" : "http://");';

echo' document.write(unescape("%3Cscript src=\'" + iproto + "static-sb.com/js/sb-tracker.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var siteid = "'.$publicKey.'" ;
var orderemail = "john@doe.com" ; /*à remplacer*/
var orderamount = 1 ; /*à remplacer*/
var ordername = "John Doe" ; /*à remplacer*/
var idorder = "'.$_SESSION['order_id'].'" ; /*à remplacer*/
var orderoptin = 1 ;
var orderstate = 10 ;
var ordertest = 0 ;
</script>
';
echo "<h2>Commande ".$_SESSION['order_id']." créé</h2>";

}

elseif (isset($_POST['old_orderid']))

{


// Mise à jour des informations d'une commande via API
// préparation des données
$post_data = array();
$post_data['request']['order_idorder'] = $_POST['old_orderid'];
if ($_POST['order_state']!='')

{

$post_data['data']['order_state'] = $_POST['order_state'];

}


if ($_POST['order_amount']!='')

{

$post_data['data']['order_amount'] = $_POST['order_amount'];

}

if ($_POST['order_num']!='')

{

$post_data['data']['order_num'] = $_POST['order_num'];

}
if ($_POST['order_idorder']!='') // mise à jour de l'order_idorder

{

$post_data['data']['order_idorder'] = $_POST['order_idorder'];
$_SESSION['order_id'] = $_POST['order_idorder'];

}
$post_data['public'] = $_SESSION['publicKey'];

// ajout de la clé publique
ksort($post_data); // par convention, on tri toujours avant de signer
$curl_post_data = http_build_query($post_data);

// Création de la signature
$sign = sha1($curl_post_data . $_SESSION['privateKey']);

echo "<p>Chaine signée : ".$curl_post_data . $_SESSION['privateKey']."<br/>Signature : ".$sign."</p>";

// finalisation des valeurs postés
$curl_post_data .= '&sign' . '='. $sign;

// Appel de l'API pour mettre à jour des informations
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://social-sb.com/api/UpdateOrder");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$curl_post_data );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_return = curl_exec($curl);
curl_close($curl);
echo "<h2>Retour appel API</h2><pre>";
echo $curl_return;
echo "</pre>";

}

else

{

// bouton pour la création d'une commande
echo '<h2>Création d\'un commande pour le client john@doe.com</h2>
<form method="post" action="">
<INPUT type="hidden" name="testCreationCde" value="1">
<INPUT type="submit" value="Création commande">
</form>';

}

if (isset($_SESSION['order_id']) && $_SESSION['order_id'])

{

// formulaire pour changer un ID Commande ou l'état
echo '<h2>Changement des information de la commande : '.$_SESSION['order_id'].'</h2>
<form method="post" action="">
<INPUT type="hidden" name="old_orderid" value="'.$_SESSION['order_id'].'"><br/>
Nouveau numéro de commande : <INPUT type="text" name="order_idorder" value=""><br/>
Numéro de facture : <INPUT type="text" name="order_num" value=""><br/>
Etat de la commande : <select name="order_state">
<option value="">-</option>
<option value="0">Commande annulée</option>
<option value="10">Commande enregistrée</option>
<option value="20">Commande payée</option>
<option value="50">Commande expédiée</option>
<option value="100">Commande reçue</option>
</select> <br>
<INPUT type="submit" value="Enregistrer">
</form>';

}

?>

</body>

</html>

  • No labels