Upload file using Guzzle 6 to ebay FEED_API endpoint - file-upload

I'm tryng to upoad e file with ebay FEED_API using Guzzle 6.5 for FILE-EXCHANGE migration to REST API:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore),
'Content-Type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'fileName',
'contents' => basename(/path/to/file)
],
[
'name' => 'name',
'contents' => 'file'
],
[
'name' => 'type',
'contents' => 'form-data'
],
[
'name' => 'file',
'contents' => fopen(/path/to/file, 'r'),
'filename' => basename(/path/to/file)
]
]
]);
but i get the following error:
[errors] => Array
(
[0] => stdClass Object
(
[errorId] => 2003
[domain] => ACCESS
[category] => APPLICATION
[message] => Internal error
[longMessage] => There was a problem with an eBay internal system or process. Contact eBay developer support for assistance
[parameters] => Array
(
[0] => stdClass Object
(
[name] => code
[value] => 400
)
[1] => stdClass Object
(
[name] => message
[value] => HTTP 400 Bad Request
)
)
)
)
but if i try to upload the file by cli using curl it works:
curl -g -k -X POST "https://api.ebay.com/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file" -H "Content-Type: multipart/form-data" -H "Authorization: Bearer v^1.1#i^1#r^0#f^0#p^3#I^3#t..." -i -F "file=#/path/to/file"
It seems api endpoint does not recognize the file associated with the form when i'm using guzzle for the request.
Where am i wrong?
--- EDIT ---
even so it works:
$headers = [
'Authorization:Bearer ' . $this->getToken($ebayStore),
'Content-Type:multipart/form-data'
];
$formData = [
'fileName' => basename($file),
'name' => 'file',
'type' => 'form-data',
'file' => curl_file_create($file)
];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, self::API_REQUEST_URI.$taskid.'/upload_file');
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_MAXREDIRS , 10);
curl_setopt($connection, CURLOPT_HEADER, 1);
curl_setopt($connection, CURLINFO_HEADER_OUT, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $formData);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);

My mistake was to manually insert the content-type in the headers.
This is my working code:
My mistake was to manually insert the content-type in the headers.
This is my working code:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore)
],
'multipart' => [
[
'name' => 'file',
'contents' => fopen($file, 'r'),
'filename' => basename($file),
'headers' => [
'Content-Disposition' => 'form-data; name="file"; fileName="'.basename($file).'"; type="form-data"'
]
]
]
]);

Related

Shopware - How to get products using App Credentials?

I created App in Shopware and able to get apiKey, secretKey, shopUrl and shopId. I want to get all my products using these credentials.
This is my code
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "http://shopware.example.com/store-api/product",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json",
"sw-access-key: SW234C1LVA010CWHDD34WK1TMW"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response, true);
print_r($data);
}
and I am getting error like this
Array
(
[errors] => Array
(
[0] => Array
(
[status] => 412
[code] => FRAMEWORK__ROUTING_SALES_CHANNEL_NOT_FOUND
[title] => Precondition Failed
[detail] => No matching sales channel found.
[meta] => Array
(
[parameters] => Array
(
)
)
)
)
)
When I using API key from settings page API I am able to get products.
But how to use App credentials (App key, secret key, shop URL) and get products
You should have gotten the API key during App registration:
https://developer.shopware.com/docs/guides/plugins/apps/app-base-guide#confirmation-request
Did you try that one? If it does not work for the store API you might want to use the admin API.

Add Tracking API in Paypal - Authorization failed

I am trying to add tracking number and order status in my sandbox account through tracking API but getting "Authorization failed due to insufficient permissions" error. Anyone have any idea why this error happens? Below are my codes and response from paypal -
API request
$clientId = 'my client id';
$secretKey = 'my secret key';
$token = $this->getAccessToken($clientId, $secretKey);
$transaction = $this->_objectManager->create('\Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory')->create()->addOrderIdFilter($currentOrder->getId())->getFirstItem();
$transactionId = $transaction->getData('txn_id');
$postfields = [
'trackers' =>
[
0 =>
[
'transaction_id' => $transactionId,
'tracking_number' => $currentOrderTrackingNumber,
'status' => 'SHIPPED',
'carrier' => 'OTHER',
'carrier_name_other' => 'SCS Logistics'
]
]
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-m.sandbox.paypal.com/v1/shipping/trackers-batch",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($postfields),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
Response from paypal
Array
(
[tracker_identifiers] => Array
(
)
[errors] => Array
(
[0] => Array
(
[name] => NOT_AUTHORIZED
[message] => Authorization failed due to insufficient permissions
[details] => Array
(
[0] => Array
(
[field] => /trackers/1/transaction_id
[value] => 42L93730906985049
[location] => body
[issue] => USER_NOT_AUTHORIZED
[description] => You are not authorized to add or modify tracking number for this transaction
)
)
[links] => Array
(
)
)
)
[links] => Array
(
[0] => Array
(
[href] => https://api-m.sandbox.paypal.com/v1/shipping/trackers-batch
[rel] => self
[method] => POST
[encType] => application/json
)
)
)
I already contacted support and they added required permission but it's not working. Please if anyone have any idea about it help me. Thanks in advance.

How to sending many POST requests with cURL faster

I have code to active various gaming platforms for player, the code is working well but it runs very slowly. How can I make it better and run faster?
<?php
$_SESSION['username'] = "demo123";
$password = "pass123";
$prefix = "HAN";
$gmusername = $prefix.$_SESSION['username'];
$gmpassword = $password;
$apikey = "SEFOQVBJOjEyMzQ1Njc4";
/////// WM ///////
$params = ['username' => $gmusername];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.gmaster8.com/WM/player/active",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $apikey"
) ,
CURLOPT_USERPWD => "$gmusername:$gmpassword",
));
curl_exec($curl);
/////// EVOLUTION ///////
$params = ['username' => $gmusername];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.gmaster8.com/EVOLUTION/player/active",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $apikey"
) ,
CURLOPT_USERPWD => "$gmusername:$gmpassword",
));
curl_exec($curl);
/////// IBC ///////
$params = ['username' => $gmusername];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.gmaster8.com/IBC/player/active",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $apikey"
) ,
CURLOPT_USERPWD => "$gmusername:$gmpassword",
));
curl_exec($curl);
////// and there are more than 20 other platforms need to be actived like that
?>
Is there any solution better to active many platforms? My code above is running without error but it takes too much time loading for player when registering and activing permision to access among gaming platforms.

app/uninstalled Webhook Registered but not showing in /webhooks.json

So basically I followed the below steps:
Make a call using PHP for app/uninstalled. Check my code.
$chh = curl_init();
$params = [
'webhook' => [
'topic' => 'app/uninstalled',
'address' => 'https://example.herokuapp.com/delete.php',
'format' => 'json'
]
];
$json_string_params = json_encode($params);
$headers = array(
'X-Shopify-Access-Token:'.$access_token,
'Content-Type:application/json'
);
curl_setopt($chh, CURLOPT_URL,
'https://example.myshopify.com/admin/webhooks.json');
curl_setopt($chh, CURLOPT_HTTPHEADER, $headers);
curl_setopt($chh, CURLOPT_HEADER, false);
curl_setopt($chh, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($chh, CURLOPT_POSTFIELDS, $json_string_params);
curl_setopt($chh, CURLOPT_RETURNTRANSFER, true);
$resultt = curl_exec($chh);
$final = json_decode($resultt);
I'm getting below response:
stdClass Object
(
[webhook] => stdClass Object
(
[id] => 475440480367
[address] => https://example.herokuapp.com/delete.php
[topic] => app/uninstalled
[created_at] => 2019-01-18T05:04:20-05:00
[updated_at] => 2019-01-18T05:04:20-05:00
[format] => json
[fields] => Array
(
)
[metafield_namespaces] => Array
(
)
)
)
Then, I'm calling this:
https://example.myshopify.com/admin/webhooks.json
but this will give me null that you haven't registered anything yet.

JIRA expand = changelog not working

I'm trying to query jira for data with folowing jql:
$jql_options = array(
"jql" => $jql,
"startAt" => 0,
"maxResults" => 1,
"validateQuery" => false,
"fields" => array(
"summary",
"status",
"assignee"
),
"expand" => "changelog"
);
And i get this error:
string(282) "{"errorMessages":["Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token\n at [Source: org.apache.catalina.connector.CoyoteInputStream#464d8928; line: 1, column: 2] (through reference chain: com.atlassian.jira.rest.v2.search.SearchRequestBean[\"expand\"])"]}"
I understand it's an error with "expand" => "changelog" but on their site they present an requeste representation like mine.
Can u give me some advice? :)
It seems i was reading the rest api documentation wrong.
"expand" option was available only in get request.
I appended jql data in url + "&expand=changelog" and other options i send them in post!
$jql_options = array(
"startAt" => 0,
"maxResults" => 1,
"validateQuery" => false,
"fields" => array(
"key"
)
);
$data = json_encode($jql_options);
$options = array(
CURLOPT_URL => $this->jiraURL . "/rest/api/latest/search?jql=" .urlencode($jql) . "&expand=changelog",
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ' . base64_encode($this->username.":".$this->password),
'Content-type: application/json') ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);