I am making a curl request to Amazon MWS orders API to get ListOrders then I loop through the xml response using foreach loop and prints the response in html table.
But as per Amazon MWS Orders API it only returns 100 results in one request and to get more results I need to make another curl request by using other parameter NextToken that I got from the last response of previous request which then will return next 100 orders and so on until there is no more NextToken available.
So my question is how can I iterate to the new response of NextToken request again and again and print the response in html table until there is no more NextToken available?
Report response is also avilable in XML format
For those who is still struggling and want an easy way to get orders data. Please use Amazon MWS Reports API and request a report using -
ReportType:_GET_XML_ALL_ORDERS_DATA_BY_ORDER_DATE_
For more info see
https://docs.developer.amazonservices.com/en_UK/reports/Reports_ReportType.html
This is what I use to convert the report's csv response to an array for saving into database.
$report_listing = explode("\n", $report_data["report_data"]);
$orders_list = '';
$i = 0;
$headers = '';
// Building an Associative array of CSV report
foreach($report_listing as $listing)
{
if($i == 0)
{
$headers = explode("\t", trim($listing));
}
else
{
$csv_data = explode("\t", $listing);
if(!isset($csv_data[1]))
continue;
foreach($headers as $key => $index)
{
$orders_list[$i - 1][$index] = $csv_data[$key];
}
}
$i++;
}
$new_order = array();
// Combining order items into one order array
foreach($orders_list as $orders)
{
$new_order[$orders['amazon-order-id']][] = $orders;
}
Related
I'm using the ZohoCRM PHP SDK to attempt to pull all Account records from the CRM and manipulate them locally (do some reports). The basic code looks like this, which works fine:
$account_module = ZCRMModule::getInstance('Accounts');
$response = $account_module->getRecords();
$records = $response->getData();
foreach ($records as $record) {
// do stuff
}
The problem is that the $records object only has 200 records (out of about 3000 total). I can't find any docs in the (minimally / poorly documented) SDK documentation showing how to paginate or get bigger result sets, and the Zoho code samples in the dev site don't seem to be using the same SDK for some reason.
Does anyone know how I can paginate through these records?
The getRecords() method seems to accept 2 parameters. This is used within some of their examples. You should be able to use those params to set/control pagination.
$param_map = ["page" => "20", "per_page" => "200"];
$response = $account_module->getRecords($param_map);
#dakdad was right that you can pass in the page and per page values into the param_map. You also should use the $response->getInfo()->getMoreRecords() to determine if you need to paginate. Something like this seems to work:
$account_module = ZCRMModule::getInstance('Accounts');
$page = 1;
$has_more = true;
while ($has_more) {
$param_map = ["page" => $page, "per_page" => "200"];
$response = $account_module->getRecords($param_map);
$has_more = $response->getInfo()->getMoreRecords();
$records = $response->getData();
foreach ($records as $record) {
// do stuff
}
$page++;
}
I am trying to update a google spreadsheet using PHP. Currently the code reliably connects and prints values, but when I try to update values, I get:
Fatal error: Uncaught exception 'Google_Exception' with message '(update) missing required param: 'spreadsheetId''
$service = new Google_Service_Sheets($client);
$spreadsheetId = '[MyID]';
$range = 'Sheet1!A2:E';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
print "No data found.\n";
} else {
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s<br>", $row[0], $row[4]);
}
}
$range = 'Sheet1!A2:E2';
$values = [1,2,3,4,5];
$body = new Google_Service_Sheets_ValueRange(['values'=>$values]);
$service->spreadsheets_values->update($spreadsheetId,'Sheet1!A2:E',$body,'raw');
The get() call works perfectly, using the same spreadsheet ID. The update call says that it is missing the spreadsheet ID parameter, but prints the correct spreadsheet ID in the call stack.
Is there an issue with the way I am passing the ID in the update call?
Issue was not actually with spreadsheet ID, but with the way $values and the value input option was passed in. $values should be a two dimensional array, and value input option should be an array not a string. posted the corrected parts of code below for posterity.
$range = 'Sheet1!A2:E2';
$values = [[1,2,3,4,5]];
$inputoption = ['valueInputOption' => "RAW"];
$body = new Google_Service_Sheets_ValueRange(['values'=>$values]);
$service->spreadsheets_values->update($spreadsheetId,$range,$body,$inputoption);
It looks like you are not passing any Oauth2 login credentials, which if I'm not mistaken, is required to add or update information through a Google API (though reading the information does not require this).
https://developers.google.com/sheets/api/quickstart/php
OR, you are not passing the correct information in $value when trying to update (it may be looking for a specific spreadsheet ID, not a range.
Hello fellow developers,
I have been trying to manipulate the output and display the total amount of workers there are instead of outputting the workers name as a string.
Bellow you will find the data that i am receiving and further down i will explain how i would like to handle the JSON response.
{
"result":
{
"addr":"ADDRESS_HERE",
"workers":
[
["worker1080",{},2,1,"200000",0,22],
["worker1080",{"a":"899.4"},3,1,"512",0,24]
],
"algo":-1
},
"method":"stats.provider.workers"
}
So basically as you can see from the above response that there are 2 workers named "worker1080" active on that address.
The bellow php code is how i retrieve the data and output only the names of the workers:
<?php
$btcwallet = get_btc_addy();
if (isset($cur_addy)) {
$method4 = new methods();
$worker_stats = new urls();
$get_data = file_get_contents(utf8_encode($worker_stats->nice_url.$method4->m4.$cur_addy));
$get_json = json_decode($get_data, true);
foreach ($get_json['result']['workers'] as $v) {
$i = 0;
print $v[$i++]."<br />";
}
}
?>
$get_json is the variable that decodes the data from $get_data and displays the worker names and increments every time a worker is added or online.
now i currently have 2 workers online as shown in the JSON response.
it outputs:
worker1080
worker1080
which is perfect although if i try using a foreach statement and try to display the the total amount of workers online it should display 2 instead of the names, it has to also increment for each worker that the json repsonse outputs.
EG: i have 2 workers online now, but in an hour i will connect 10 more it would display the following:
worker1080
worker1080
worker1070
worker1070
worker1080ti
worker1080ti
workerASIC
workerASIC
workerASIC
workerCPU
workerCPU
workerCPU
Now i try to use the following to display the total:
count($v[$i++]);
and i have tried using a foreach within the foreach, and both count and the foreach both will either display "0" by all the workers or "1"
bellow is an example of the output.
0
0
0
0
0
How would i go about counting each line in the output and display the total number of workers ?
Thanks #symcbean for the solution.
<?php
$btcwallet = get_btc_addy();
if (isset($cur_addy)) {
$method4 = new methods();
$worker_stats = new urls();
$get_data = file_get_contents(utf8_encode($worker_stats->nice_url.$method4->m4.$cur_addy));
$get_json = json_decode($get_data, true);
print count($get_json['result']['workers'])."<br />"; // <-- solution *removed foreach and $i incrementation as its not needed for count
}
?>
it now displays the correct number of workers :)
Hello I am trying to extract some info from a json format page
The page is : https://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF
this link allows to get all bitcoin transactions from an address.
All the quotation marks confuse me, I can not see clearly
I want to display everything like the original blockchain website
Blockchain Display view
The beginning will be something like that
$json = file_get_contents("https://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
var_dump(json_decode($json));
I can extract basic info from JSO to php, but here there is too much transactions and I think we need to use a loop to display everything but I don't know how to do that
If someone can display for me in php the 5 first transactions it will be very sympathic.
Thanks a lot in advance youw ill really help me if you can do that !
If you goal is to display all Tx info you want to do that.
$json = file_get_contents("http://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
$txs = json_decode($json,1)['txs'];
echo"<pre>"; //just to get a human readable display
foreach($txs as $txinfo){
echo"A new tx";
//will display the full tx data
print_r($txinfo);
}
echo"</pre>";
Note a blockchain transaction can be a little complexe since one transaction can have multiple input and multiple outputs.
you might want to cycle trough outputs as well to display all addresses who received bitcoin from a transaction
in that case you can simply add another foreach output
$json = file_get_contents("http://blockchain.info/fr/rawaddr/1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF");
$txs = json_decode($json,1)['txs'];
echo"<pre>"; //just to get a human readable display
foreach($txs as $txinfo){
echo"A new tx";
//will display the full tx data
print_r($txinfo);
// will cycle trough all output for each tx
foreach ($txinfo['out'] as $outgoingTransaction)
{
//Will display the receiving address
$receivingAddress = $outgoingTransaction['addr'];
//will get the amount sent in satoshis
$receivingAmountInSatoshi = $outgoingTransaction['value'];
echo"<br>1BQLNJtMDKmMZ4PyqVFfRuBNvoGhjigBKF sent to $receivingAddress $receivingAmountInSatoshi satoshis <br>";
}
}
a more advanced code to add tx understanding logic
$walletAddress = '1AWKFrvFYuCC7ef2m2zX73pWu1C15FRGjR' ;
$json = file_get_contents("http://blockchain.info/fr/rawaddr/$walletAddress");
$txs = json_decode($json,1)['txs'];
echo"<pre>"; //just to get a human readable display
foreach($txs as $txinfo){
$spendingTx = false ;
$totalSpent = 0 ;
$totalReceived = 0;
echo"<p>Txid = $txinfo[hash]<br>";
//print_r($txinfo);
// we need to find out if the address is the sender or the receiver
$senderData = reset($txinfo['inputs']); //using reset to get only the first input
if ($senderData['prev_out']['addr'] ==$walletAddress ){
//the address is the sender meaning the address is spending
$spendingTx = true ;
}
//it's a spend tx then we cycle trough receivers
if ($spendingTx) {
foreach ($txinfo['out'] as $outgoingTransaction) {
//Will display the receiving address
$receivingAddress = $outgoingTransaction['addr'];
//will get the amount sent in satoshis
$receivingAmountInSatoshi = $outgoingTransaction['value'];
$totalSpent = $totalSpent + $receivingAmountInSatoshi ;
echo "<br>$walletAddress sent to $receivingAddress $receivingAmountInSatoshi satoshis <br>";
}
echo "<br>Total spent = $totalSpent" ;
}
//it is not a spending tx so it's a receceiving tx
else {
foreach ($txinfo['out'] as $outgoingTransaction) {
//We keep only receiving data concerning current wallet
if ($outgoingTransaction['addr'] == $walletAddress) {
//Will display the receiving address
$receivingAddress = $outgoingTransaction['addr'];
//will get the amount sent in satoshis
$receivingAmountInSatoshi = $outgoingTransaction['value'];
$senderAddress = $senderData['prev_out']['addr'];
$totalReceived = $receivingAmountInSatoshi;
echo "<br>$walletAddress received $receivingAmountInSatoshi satoshis from $senderAddress<br>";
}
}
echo "<br>Total received = $totalReceived" ;
}
echo"<br>end tx </p>";
}
echo"</pre>";
I have created a facebook application for integrating facebook credits.It is working fine but i have some doubts regarding on that
In call back payment get items function i have used this code for assigning order information
if ($func == 'payments_get_items') {
// remove escape characters
$order_info = stripcslashes($payload['order_info']);
if (is_string($order_info)) {
$item = json_decode($order_info, true);
$item['item_id'] = intval($item['GreeID']);
}
}
I want to get the item_id in 'payments_status_update update function.I have used $payload['order_details'] method .I am getting all the values in a json format.But when i use json_decode the purchase is not working properly.Is their any other method to get the item_id inside payments_status_update function
if ($func == 'payments_status_update')
{
$payload['order_details']
}
$order_info = json_decode(preg_replace('/:(\d+)/', ':"${1}"',stripcslashes($payload['order_details'])),true);