How to generate the _aop_signature for Aliexpress API using PHP? - php

I'm using the Aliexpress API generatePromotionLinks. This API requires an parameter which is _aop_signature
URL is this :
https://gw.api.alibaba.com/openapi/param2/1/portals.open/api.generatePromotionLinks/[appKey]?trackingId=[trackingId]&targetUrls=[url]&locale=[global]&linkType=HOT_PRODUCT_LINK&_aop_signature=[signature]
I want to know where I can get the _aop_signature, or how to generate the _aop_signature using PHP.

<?php
$url = 'https://gw.api.alibaba.com/openapi/';
$appKey = ***;
$appSecret ='***';
$apiInfo = 'param2/1/portals.open/api.generatePromotionLinks/' . $appKey;
$code_arr = array(
'urls' => 'https://ru.aliexpress.com/af/mp3.html?d=y&origin=n&SearchText=mp3&catId=0&initiative_id=SB_20191106040548',
'linkType' => 'SEARCH_LINK',
'fields' => 'promotionUrls,trackingId,publisherId',
'trackingId' => 'Please create a unique affiliate ID for your site(s).',
);
$aliParams = array();
foreach ($code_arr as $key => $val) {
$aliParams[] = $key . $val;
}
sort($aliParams);
$sign_str = join('', $aliParams);
$sign_str = $apiInfo . $sign_str;
$code_sign = strtoupper(bin2hex(hash_hmac("sha1", $sign_str, $appSecret, true)));
$url = $url.$apiInfo.'?'.http_build_query($code_arr)."&_aop_signature=".$code_sign;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
?>

Related

Php curl Jira issue creation not working with custom field

All , I am using PHP to create jira issues , below code is working fine , only issue if I sends custom fields value it sending error. please let me know if I am doing it in wrong way.
function createJiraServiceDeskRequestWithAttachment($serviceDeskId,$requestTypeId,$summary,$description = '', array $files = [],
$url,$user, $token, $primaryCustomerRepId=10202, $isAttachmentPublic = true
): array {
$returnValue = ['ticket' => [], 'temporaryAttachmentIds' => [], 'attachments' => [], 'error' => null];
// make sure to remove additional forward slashes /
$url = rtrim($url, '/');
$endPoint = $url . '/rest/servicedeskapi/request';
$data = [
"serviceDeskId" => $serviceDeskId,
"requestTypeId" => $requestTypeId,
"requestFieldValues" => [
"summary" => $summary,
"description" => $description,
// "customfield_10100" => $primaryCustomerRepId
],
];
try {
$ch = curl_init($endPoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$jsonData = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$issueResult = curl_exec($ch);
$issueResult = json_decode($issueResult, true);
if (curl_errno($ch) || empty($issueResult))
return $returnValue;
curl_close($ch);
$returnValue['ticket'] = $issueResult;
// here add temporary attachment
if (! empty ($files)):
$uploadedAttachments = [];
$endPoint = $url . "/rest/servicedeskapi/servicedesk/{$serviceDeskId}/attachTemporaryFile";
foreach ($files as $index => $file) {
$ch = curl_init($endPoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => curl_file_create(
realpath($file),
mime_content_type($file),
basename($file)
)]);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data', 'X-Atlassian-Token: no-check']);
$tmpResult = curl_exec($ch);
if (! curl_errno($ch) && ! empty($tmpResult = json_decode($tmpResult, true))) {
if (isset($tmpResult['temporaryAttachments']) && isset($tmpResult['temporaryAttachments'][0]['temporaryAttachmentId']))
$uploadedAttachments[] = $tmpResult['temporaryAttachments'][0]['temporaryAttachmentId'];
}
curl_close($ch);
}
else:
return $returnValue;
endif;
// check if there are temporary attachments uploaded, then link them with the request
if (! empty($uploadedAttachments)):
$returnValue['temporaryAttachmentIds'] = $uploadedAttachments;
$endPoint = $url . "/rest/servicedeskapi/request/{$issueResult['issueId']}/attachment";
$ch = curl_init($endPoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$jsonData = json_encode(["temporaryAttachmentIds" => $uploadedAttachments, 'public' => $isAttachmentPublic]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$attachresult = curl_exec($ch);
$attachresult = json_decode($attachresult, true);
if (! curl_errno($ch) && ! empty($attachresult))
$returnValue['attachments'] = $attachresult;
curl_close($ch);
return $returnValue;
else:
return $returnValue;
endif;
} catch (\Exception $e) {
$returnValue['error'] = $e->getMessage();
return $returnValue;
}
}
If I uncomment this code customfield_10100, it doesn't work
I also tried to place it outside of requested values array
"fields" =>[
"customfield_10100" => $primaryCustomerRepId
]

PHP API Paging loop

I have this code
I need a loop to be able to print all results which are 400-600 but I'm not able because I'm limited to only 100 shows per page and I need a loop/while to make the same request but changing the page and then print the result and I'm not able to make it, can someone explain me how to implement here a loop or to give me some examples?
<?php
include('config/config.php');
$ch = curl_init();
$username = '**************';
$password = ''**************';';
$hash = base64_encode($username . ':' . $password);
$headers = array(
'Authorization: Basic ' . $hash
);
$query = $connection->query("SELECT id_xyz_ro as prods FROM stock_prods where id_xyz_ro > 0");
$array = Array();
while($result = $query->fetch_assoc()){
$array[] = $result['prods'];
}
$connection->query('SET session group_concat_max_len=15000');
foreach($connection->query("SELECT GROUP_CONCAT(CONCAT('\'', id_xyz_ro, '\'')) as prods FROM stock_prods where id_xyz_ro > 0") as $row) {
}
$datas = array(
'id' => $array,
'itemsPerPage' => '100', //Only 100 because API it's limiting me at 100 shows per page
'currentPage' => '1' //Here I need to loop
);
$data = array(
'data' => $datas
);
set_time_limit(0);
$url = "www.xyz.net/api-3/product_offer/read";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result); //After loop I need to print results from all loops
?>
Thanks in advance

How to create local post on google my business page using php

I need to create a local host using google client api . I wrote following code to crate local post.
$service = new Google_Service_MyBusiness($client);
$callToAction = new Google_Service_MyBusiness_CallToAction();
$callToAction->setActionType('LEARN_MORE');
$callToAction->setUrl('localhost.com');
$mediaItem = new Google_Service_MyBusiness_MediaItem();
$mediaItem->setMediaFormat('PHOTO');
$mediaItem->setSourceUrl('https://www.theanthem.com/images/usps_eddm_postcards.jpg');
$localPost = new Google_Service_MyBusiness_LocalPost();
$localPost->setLanguageCode('en-US');
$localPost->setSummary('Just a summary');
$localPost->setCallToAction($callToAction);
$localPost->setMedia($mediaItem);
$parent = sprintf('accounts/%d/locations/%d',
'116633170334837786295',
'8830395735149945322'
);
$service->accounts_locations_localPosts->create($parent, $localPost);
But Unfortunately I got following error.
My Error: Message: Error calling POST
https://mybusiness.googleapis.com/v4/accounts/9223372036854775807/locations/8830395735149945322/localPosts:
(400) Request contains an invalid argument.
How can i fix this?
I found the answer:
create refresh token
$request_url = "https://www.googleapis.com/oauth2/v4/token";
$refresh_token = "*** Refresf Token ***";
$params = [
'client_id' => "***your client id***",
'client_secret' => "***your clinet secret id***",
'refresh_token' => $refresh_token,
'grant_type' => "refresh_token"
];
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v) {
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$json_response = curl_exec($curl);
$response = (array) json_decode( $json_response );
$myaccess_token = $response['access_token'];
And create Post
$location = 'accounts/accoutid/locations/location id';
$api_end_point_url = 'https://mybusiness.googleapis.com/v4/'.$location.'/localPosts';
$postfields = array(
'topicType' => "STANDARD",
'languageCode' => "en_US",
'summary' => 'test post 123',
);
$data_string = json_encode($postfields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_end_point_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $myaccess_token,'Content-Type: application/json'));
$data1 = json_decode(curl_exec($ch), true);
$http_code1 = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Pipedrive delete a deal with API

I am trying to delete a deal using through the API. The code I have written is below , but its not working. I am not able to figure out where to add the Method "DELETE" while making the call.I am not getting any error message in output. Please suggest.
<?php
$api_token = "myapitoken";
$url = "https://api.pipedrive.com/v1/deal?api_token=" . $api_token;
$deal = array(
'id' => 375,
'method' => 'DELETE'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$result = json_decode($output);
?>
Deleting deal on pivedrive can be done using following code
$id= "deal_id";
$url = "https://api.pipedrive.com/v1/deals/". $id ."?api_token=" . $api_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Your api token is a postfield too.
Maybe this way:
$url = "https://api.pipedrive.com/v1/deal";
$deal = array(
'api_token'=> $api_token,
'id' => 375,
'method' => 'DELETE'
);

CoinBase "invalid signature" PHP Buy API Request

I have looked over the code many times but whenever I send request to API it returns "message":"invalid signature"
I am thinking it has to do with hashing the body, possibly. I'm new to PHP and its my first project. :)
Anyone see an error? Thanks.
<?php
$arr = array('size' => ".01", 'price' => '240', 'side' => 'sell',
'product_id' => 'BTC-USD');
$output = json_encode($arr);
echo json_encode($arr)."<br/>";
$key = "f23612b06cb4d020cda7e04b1ae6ef9a";
$secret = "RENqodtuTCn4v7g7Pn/FFdQAIKReVXGayNPrNN/Zb7AjATI0hP4R0MCDD5RqnDu60qTZ5Qry329fFu7kcObGBw==";
$passphrase = "tradebot";
$time = time();
$url = "https://api.gdax.com/orders";
$data = $time."POST"."/orders";
echo $data . "<br/>";
$hashinput = "$output"."$data";
$sign = base64_encode(hash_hmac("sha256", $hashinput, base64_decode($secret), true));
echo $sign . "<br/>";
$headers = array(
'CB-ACCESS-KEY: '.$key,
'CB-ACCESS-SIGN: '.$sign,
'CB-ACCESS-TIMESTAMP: '.$time,
'CB-ACCESS-PASSPHRASE: '.$passphrase,
'Content-Type: application/json'
);
var_dump($headers);
echo $url;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'local server');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, $output);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
echo $res;
}
replace
$hashinput = "$output"."$data";
with
$hashinput = "$data"."$output";
and replace
curl_setopt($ch, CURLOPT_POST, $output);
with
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
(from Coinbase Community forum)

Categories