A1
<?php
class Api {
private static $apiToken = '';
private static $merchantId;
public $limit = 50;
public $version = "v2/";
public $baseProdPath = "https://china-merchant.wish.com/api/";
public $baseStagePath = "https://merch.corp.contextlogic.com/api/";
public function __construct($apiToken, $merchantId = '') {
$this->apiToken = $apiToken;
$this->merchantId = $merchantId;
}
public function getVersion() {
return $this->version;
}
public function getRequestUrl() {
return $this->baseProdPath;
}
public function requestHost($method, $path, array $params) {
$params['access_token'] = $this->apiToken;
$url = $this->getRequestUrl() . $this->getVersion() . $path;
$curl = curl_init();
$options = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
CURLOPT_USERAGENT => 'wish-php-sdk',
CURLOPT_HEADER => 'true'
);
if ($method === "GET") {
$url = $url . "?" . http_build_query($params);
} else {
$options[CURLOPT_POSTFIELDS] = $params;
}
$options[CURLOPT_URL] = $url;
curl_setopt_array($curl, $options);
$i = 0;
do {
$result = curl_exec($curl);
if ($result || $i++ > 2) {
break;
}
} while (true);
curl_close($curl);
return $result;
}
public function getAllChangedOrdersSince($page = 0, $time = '') {
$params = array();
if ($time) {
$params['since'] = $time;
}
$params['limit'] = $this->limit;
if ($page > 0) {
$params['start'] = $page * $this->limit;
} else {
$params['start'] = $page;
}
return $this->requestHost('GET', 'order/multi-get', $params);
}
public function getAllUnfulfilledOrdersSince($page = 0, $time = '') {
$params = array();
if ($time) {
$params['since'] = $time;
}
$params['limit'] = $this->limit;
if ($page > 0) {
$params['start'] = $page * $this->limit;
} else {
$params['start'] = $page;
}
return $this->requestHost('GET', 'order/get-fulfill', $params);
}
public function fulfillOrder($orderId, $provider, $trackNumber, $memo = '') {
$params = array(
'tracking_provider' => $provider,
'tracking_number' => $trackNumber,
'ship_note' => $memo,
'id' => $orderId,
'origin_country_code' => 'CN',
);
return $this->requestHost('POST', 'order/fulfill-one', $params);
}
}
?>
A2
class Api
{
private $api_key = '***********32位**********';
private $api_url = 'https://www.wishpost.cn/api/v2/create_order';
private $order_status_url = 'https://www.wishpost.cn/api/v3/order_status';
public function Create_an_order($trades_id, $otype, $has_battery)
{
global $db, $ass;
$receive = $receive;
$account = $account;
$address = $address;
$orders = $orders;
$goods = $goods;
if ($otype == '5001-1' || $otype == '5002-1') {
$doorpickup = 0;
} else {
$doorpickup = 1;
}
$Parameters = array(
'access_token' => $account['lgs_access_token'],
'api_key' => $this->api_key,
'order' => array(
'guid' => 200,
'otype' => $otype,
'from' => $receive['address_name'],
'sender_province' => $receive['province_name'],
'sender_city' => $receive['city_name'],
'sender_addres' => $receive['address'],
'sender_phone' => $receive['phone'],
'sender_zipcode' => $receive['postcode'],
'pickup_from_local' => $receive['address_name'],
'pickup_phone' => $receive['phone'],
'pickup_zip_code' => $receive['postcode'],
'pickup_addres_local' => $receive['address'],
'pickup_province_local' => $receive['province_name'],
'pickup_city_local' => $receive['city_name'],
'pickup_from' => 'xxxxx',
'pickup_addres' => 'xxxx',
'pickup_province' => 'Guangdong Province',
'pickup_city' => 'Shenzhen',
'pickup_country_local' => "中国",
'pickup_country' => 'China',
'pickup_country_code' => 'CHN',
'to' => $address['ShipName'],
'recipient_country' => $address['ShipCountryEn'],
'recipient_country_short' => $address['ShipCountry'],
'recipient_province' => $address['ShipRegion'],
'recipient_city' => $address['ShipCity'],
'recipient_addres' => $address['ShipLine'],
'recipient_postcode' => $address['ShipPostalCode'],
'recipient_phone' => $address['ShipPhone'],
'to_local' => $address['ShipName'],
'recipient_country_local' => $address['ShipCountryEn'],
'recipient_province_local' => $address['ShipRegion'],
'recipient_city_local' => $address['ShipCity'],
'recipient_addres_local' => $address['ShipLine'],
'type_no' => 3,
'from_country' => 'China',
'content' => $goods['goods_declaredname'],
'content_chinese' => $goods['goods_sku'] . $goods['goods_declaredcnname'],
'num' => $orders['QuantityOrdered'],
'unit_measurement' => 'item',
'unit_measurement_chinese' => "个",
'weight' => ($orders['QuantityOrdered'] * $goods['weight']) / 1000,
'single_price' => 17,
'trande_no' => $order_no,
'trade_amount' => $address['order_money'],
'user_desc' => $goods['goods_sku'],
'receive_from' => $receive['address_name'],
'receive_province' => $receive['province_name'],
'receive_city' => $receive['city_name'],
'receive_addres' => $receive['address'],
'receive_phone' => $receive['mobile'],
'warehouse_code' => 3,
'doorpickup' => $doorpickup,
'has_battery' => $has_battery,
'product_url' => 'https://www.wish.com/c/' . $Order_no,
)
);
$result = json_decode(json_encode(simplexml_load_string($this->wish_curl($Parameters, $this->api_url))), true);
if (isset($result['status']) && $result['status'] == 0) {
if (is_array($result['barcode'])) {
$tracking_data = $this->get_order_status($account['lgs_access_token'], $result['wish_standard_tracking_id']);
if (strlen($tracking_data['orders']['0']['logistics_order_code']) > 3) {
$tracking = array(
"status" => $tracking_data['code'],
"barcode" => $tracking_data['orders']['0']['logistics_order_code'],
);
} else {
$tracking = array(
"status" => $tracking_data['orders']['0']['state_code'],
"barcode" => $tracking_data['orders']['0']['logistics_order_code'],
"error_message" => $tracking_data['orders']['0']['message']
);
}
return $tracking;
} else {
return $result;
}
} else {
return array(
"status" => 1,
"error_message" => $result['error_message']
);
}
}
public function get_order_status($acces_token, $wish_standard_tracking_ids)
{
$Parameters = array(
'access_token' => $acces_token,
'wish_standard_tracking_ids' => [$wish_standard_tracking_ids],
);
return json_decode($this->wish_order_status_cur($Parameters, $this->order_status_url), true);
}
public function arrayToXml($data)
{
if (!is_array($data) || count($data) <= 0) {
return false;
}
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">";
foreach ($val as $key2 => $val2) {
if (is_numeric($key2)) {
$xml .= "<" . $key2 . ">" . $val2 . "</" . $key2 . ">";
} else {
$xml .= "<" . $key2 . "><![CDATA[" . $val2 . "]]></" . $key2 . ">";
}
}
$xml .= "</" . $key . ">";
} else {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
}
$xml .= "</xml>";
return $xml;
}
public function wish_curl($Parameters, $url)
{
$header[] = "Content-Type: text/xml; charset=utf-8";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->arrayToXml($Parameters));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function wish_order_status_cur($Parameters, $url)
{
$header[] = "Content-Type: text/xml; charset=utf-8";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Parameters));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}