Skip to content

Instantly share code, notes, and snippets.

@antonshell
Created September 2, 2015 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonshell/e92cb9cc57e7c8555d3a to your computer and use it in GitHub Desktop.
Save antonshell/e92cb9cc57e7c8555d3a to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: Antonshell
* Date: 31.08.2015
* Time: 14:40
*/
/**
* Class WixService
*/
class WixClient{
private $app_key;
private $secret;
/**
* @param $app_key
* @param $secret
*/
public function __construct($app_key,$secret){
$this->app_key = $app_key;
$this->secret = $secret;
}
/**
* @return mixed|string
*/
public function getContactsList(){
$wixClient = new WixSDK($this->app_key,$this->secret);
$wixInfo = $wixClient->get_wix_json();
$data = $wixClient->get_contacts($wixInfo->instanceId);
$data = json_decode($data,true);
return $data;
}
/**
* @param $contactId
* @return mixed|string
*/
public function getContactById($contactId){
//$contactId = '2b56621f-be44-419b-975b-67a57a0e0082';
$wixClient = new WixSDK($this->app_key,$this->secret);
$wixInfo = $wixClient->get_wix_json();
$data = $wixClient->get_contact($wixInfo->instanceId,$contactId);
$data = json_decode($data,true);
return $data;
}
public function createContact(){
$data = [
"emails"=>[
"tag"=>"work",
"email"=>"karen_meep@wix.com",
"emailStatus"=>"recurring"
]
];
$wixClient = new WixSDK($this->app_key,$this->secret);
$wixInfo = $wixClient->get_wix_json();
$result = $wixClient->create_contact($wixInfo->instanceId,$data);
return $result;
}
}
<?php
/**
* Wix simple working php sdk
* ransom1538@gmail.com
*/
class WixSDK{
public $secret;
public $app_key;
function __construct($app_key, $secret)
{
$this->secret = $secret;
$this->app_key = $app_key;
}
public function get_signature_timestamp(){
$ts = date('Y-m-d\TH:i:s') . substr(microtime(), 1, 4) . 'Z';
return $ts;
}
public function encode_signature_custom( $instance_id, $method, $request_path, $query_params, $post_params, $body = '')
{
$request_params = array_merge($query_params, $post_params);
ksort($request_params);
$signature_string = strtoupper($method) . "\n$request_path\n";
foreach ($request_params as $request_param)
{
switch(gettype($request_param))
{
case 'boolean':
$signature_string .= $request_param ? "true\n" : "false\n";
break;
default:
$signature_string .= strval($request_param) . "\n";
break;
}
}
$signature_string = trim($signature_string);
if($body != NULL && strlen($body) > 0) {
$signature_string .= "\n" . $body;
}
$encoded_signature = strtr(base64_encode(hash_hmac("sha256", $signature_string, $this->secret , TRUE)), '+/', '-_');
while(substr($encoded_signature, -1) == '=')
{
$encoded_signature = substr($encoded_signature, 0, -1);
}
return array('sig' => $encoded_signature, 'signature_string' => $signature_string);
}
public function encode_signature( $instance_id, $method, $request_path, $query_params, $post_params, $body = '')
{
//This form of timestamp was posted on the communties however it doesnt' work
//and causes: Required parameter/header [timestamp/x-wix-timestamp] has illegal value [2014-11-10T17:23:10-06:00]
//$ts = date('Y-m-d\TH:i:s') . substr(microtime(), 1, 4) . 'Z';
$ts = $this->get_signature_timestamp();
$request_params = array_merge($query_params, $post_params);
$request_params['application-id'] = $this->app_key;
$request_params['instance-id'] = $instance_id;
$request_params['timestamp'] = $ts;
$request_params['version'] = "1.0.0";
if($body){
$request_params['version'] = "2.0.0";
}
ksort($request_params);
$signature_string = strtoupper($method) . "\n$request_path\n";
foreach ($request_params as $request_param)
{
switch(gettype($request_param))
{
case 'boolean':
$signature_string .= $request_param ? "true\n" : "false\n";
break;
default:
$signature_string .= strval($request_param) . "\n";
break;
}
}
$signature_string = trim($signature_string);
if($body != NULL && strlen($body) > 0) {
$signature_string .= "\n" . $body;
}
$encoded_signature = strtr(base64_encode(hash_hmac("sha256", $signature_string, $this->secret , TRUE)), '+/', '-_');
while(substr($encoded_signature, -1) == '=')
{
$encoded_signature = substr($encoded_signature, 0, -1);
}
return array('sig' => $encoded_signature, 'ts' => $ts);
}
public function get_wix_json()
{
list( $code, $data ) = explode( '.', $_GET[ 'instance' ] );
if ( base64_decode( strtr( $code, "-_", "+/" ) ) != hash_hmac( "sha256", $data, $this->secret, TRUE ) ) {
echo "Unable to get instance";
return false;
}
if ( ( $json = json_decode( base64_decode( $data ) ) ) === null ) {
echo "Unable to get instance";
return false;
}
return $json;
}
public function get_contacts($instance_id){
$signatures = $this->encode_signature( $instance_id, 'GET', '/v1/contacts', array(), array(), '');
$url = 'https://openapi.wix.com/v1/contacts';
$url .= "?timestamp=". ($signatures['ts']) . "&application-id=".($this->app_key)."&instance-id=". ($instance_id)."&signature=".$signatures['sig']."&version=1.0.0";;
return ($this->curl_request('GET', $url , array()));
}
public function get_contact($instance_id,$contactId){
$signatures = $this->encode_signature( $instance_id, 'GET', '/v1/contacts/'.$contactId, array(), array(), '');
$url = 'https://openapi.wix.com/v1/contacts/'.$contactId;
$url .= '?version=1.0.0&application-id='.($this->app_key).'&instance-id='.($instance_id).'&timestamp='.($signatures['ts']).'&signature='.($signatures['sig']);
return ($this->curl_request('GET', $url , array()));
}
public function create_contact($instance_id,$data){
$body = json_encode($data);
$query_params = [
'version' => '2.0.0',
'application-id' => ($this->app_key),
'instance-id' => ($instance_id),
'timestamp' => $this->get_signature_timestamp(),
];
$request_path = '/v1/contacts';
$method = 'POST';
$signatures = $this->encode_signature_custom( $instance_id, $method, $request_path, $query_params, array(), $body);
$query_params['signature'] = $signatures['sig'];
$url = 'https://openapi.wix.com' . $request_path . '?' . http_build_query($query_params);
return ($this->curl_request($method, $url, $body));
}
public function get_sites( $instance_id)
{
$signatures = $this->encode_signature( $instance_id, 'GET', '/v1/sites/site', array(), array(), '');
$url = 'https://openapi.wix.com/v1/sites/site';
$url .= "?timestamp=". ($signatures['ts']) . "&application-id=".($this->app_key)."&instance-id=". ($instance_id)."&signature=".$signatures['sig']."&version=1.0.0";;
return ($this->curl_request('GET', $url , array()));
}
public function curl_request($method, $uri, $data = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
if ('POST' == $method)
{
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if ('PUT' == $method)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if('GET' != $method)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $header_size);
return $body;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment