Not sure how to word my question, but this is it:
I have an XML with 1000 products that I need to upload. I can only upload 100 products at a time with PHP. How do I make one PHP upload 100 products at a time until all products are uploaded?
Currently, my solution is to have 10 PHP files and 10 XML files each with 100 products. I would prefer if I can do this with 1 XML and 1 PHP file.
The reason I cannot do more than 100 products is that the sever I am uploading to doesn't allow me to more than 100 per instance.
I am using simplexml_load_file to load the files in the PHP
This is my code so far:
<?php
$data = Array();
$xml=simplexml_load_file("products_1.xml") or die("Error: Cannot create object");
foreach ($xml->product as $pr) {
$data[] = Array
(
"id" => (int)$pr->identifier,
"name" => (string)$pr->name,
...
);
}
// username & password
#$data = array_slice($data, 1,10);
#$data = $data[20];
$username = '...';
$password = '...';
//create hash
$hash = base64_encode($username . ':' . $password);
$headers = array(
'Authorization: Basic ' . $hash
);
// send data with cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://.../api/product/save');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('data'=>($data))));
$result = curl_exec($ch);
// decode response
$all=json_decode($result);
// print array response
echo '<hr>';
echo '<pre>';
print_r($all);
echo '</pre>';
//echo '<hr>';
?>
For the other PHP files, I just replace the products_1.xml with the next number
Related
I have an Excel files approx. 1,29 MB and it has 11500 lines of data.
In my PHP code first i read whole files and put in arrays via PHPExcel/IOFactory library.
After the putting, in foreach loop i start to create $postvars for cUrl.
gonderi_referans=REF&alici=Sarah&alici_telefon=55544448885&alici_adres=Krg&alici_ulke=DE
In foreach loop the code will create 11500 times $postvars and make curl request.
$url = "https://$_SERVER[HTTP_HOST]/api572.php";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,30);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response =json_decode(curl_exec($ch), true);
This operation takes approx 5 minutes. I want to do it in a faster way.
How can i do it?
Code read excel files in a 10 seconds, curl request it takes lots of times.
Try to use some batch-processing to minimize the number of sent requests, totally basic POC:
excel-reader.php:
<?php
// Dummy data START
$dummyExcelRows = [];
for ($i = 0; $i <= 11500; $i++) {
$dummyExcelRows[] = "gonderi_referans=REF&alici=Sarah&alici_telefon=55544448885&alici_adres=Krg&alici_ulke=DE&id=$i";
}
// Dummy data END
// Serialize multiple URI into one string START
$data = [];
foreach ($dummyExcelRows as $postvars) {
$data[] = $postvars;
}
$dataToSend = ['payload' => json_encode($data)];
// Serialize multiple URI into one string END
$url = "http://$_SERVER[HTTP_HOST]/test/api572.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToSend);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// $response =json_decode(curl_exec($ch), true);
$response = curl_exec($ch);
var_dump($response);
api572.php:
<?php
$decoded = json_decode($_POST['payload']);
foreach ($decoded as $row) {
echo "Do something with $row <br/>";
echo '<pre>';
parse_str($row, $fakePost);
print_r($fakePost);
echo '</pre>';
}
I am developing an sms sending php system the server can only handle 100 calls per request. How can I make 100 API calls per second per request when I'm sending large data of messages of about 5000+,
here is my PHP code
$phones = $_POST['recipient_phones']; //get phone numbers from textarea
$phones = chop($phones,","); //remove last comma
$phones = multiexplode(array(","," ","\r\n",".","|",":"),$phones);//reformat and convert to array
$sender = $_POST['sender_id']; //Get Sender ID input field
$message = $_POST['message']; //Get Message input field
$link_id = $rand_link_id;
$correlator = $randCorrelator;
$endpoint = 'https://exampleurl.com';
foreach($phones as $phone){
$data = array("link_id" => "$link_id", "sender" => "$sender","phone" => "$phone", "message" => "$message","endpoint" => "$endpoint","correlator" => "$correlator");
$data_string = json_encode($data);
$ch = curl_init('https://bulk.example.com/api/v1/send-sms');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json','Accept: application/json','Authorization:Bearer '.$token,
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$smsResults = curl_exec($ch);
sleep(3);
//close connection
curl_close($ch);
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
I am struggling using Binance's REST API. I have managed to get working GET request via query string such as pinging the server, ticker information, etc. My challenge now is performing POST request via query string using cURL. I have been scraping code from various places and referring back to the API to get pieces to work but I am unsure as to why I am getting this error returned from the result... {"code":-1102,"msg":"Mandatory parameter 'signature' was not sent, was empty/null, or malformed."}
(ERROR SHOWN ON WEBPAGE). I echo out the signature and its a load of gibberish so I would believe that the hash_hmac performed at the top would be working, but honestly I got pretty lucky making the GET request work. Does anyone have any suggestions as to why this would be broken? Thanks!
$apikey = "MYKEY";
$apisecret = "MYSECRET";
$timestamp = time()*1000; //get current timestamp in milliseconds
$signature = hash_hmac('sha256', "TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000×tamp=".$timestamp, $apisecret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.binance.com/api/v3/order/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000×tamp=".$timestamp);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey,"signature: ".$signature));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
As per their API docs:
SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body.
You are sending the signature via neither of these methods and are instead sending it through the header.
Change this:
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000×tamp=".$timestamp);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey,"signature: ".$signature));
To this:
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000×tamp=" . $timestamp . "&signature=" . $signature);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey));
<?php
$secret = "F................";
$key = "D.................";
$s_time = "timestamp=".time()*1000;
$sign=hash_hmac('SHA256', $s_time, $secret);
$url = "https://api.binance.com/api/v3/account?".$s_time.'&signature='.$sign;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-MBX-APIKEY:'.$key));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$result = json_decode($result, true);
echo '<pre>';
var_dump($result);
echo '</pre>';
?>
Here is an example, using php-curl-class
// Variables
// url, key and secret is on separate file, called using require once
$endPoint = "/api/v3/order/test";
$coin = "BTC";
$fiat = "EUR";
$symbol = $coin . "" . $fiat;
$side = "BUY";
$type = "LIMIT";
$timeInForce = "GTC";
$quantity = 1;
$price = 10000;
$timestamp = time();
// Constructing query arrays
queryArray = array(
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"timeInForce" => $timeInForce,
"quantity" => $quantity,
"price" => $price,
"timestamp" => $timestamp*1000
);
$signature = hash_hmac("sha256", http_build_query($queryArray), $secret);
$signatureArray = array("signature" => $signature);
$curlArray = $queryArray + $signatureArray;
// Curl : setting header and POST
$curl->setHeader("Content-Type","application/x-www-form-urlencoded");
$curl->setHeader("X-MBX-APIKEY",$key);
$curl->post($url . "" . $endPoint, $curlArray);
if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
}
$order = $curl->response;
print_r($order);
I had the same problem, and nothing of above doesn't helped.
So I finaly figured out how to make order on my way.
So, maybe this helps someone.
function Kupovina($buy_parametri) {
$key = "xxxxxxxxxxxxxxx";
$secret = "xxxxxxxxxxxx";
$s_time = "timestamp=".time()*1000;
$timestamp = time()*1000; //get current timestamp in milliseconds
$sign = hash_hmac('sha256', $buy_parametri."×tamp=".$timestamp, $secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.binance.com/api/v3/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $buy_parametri."&".$s_time."&signature=".$sign);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$key));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$buy_parametri = "symbol=BTCUSDT&type=market&side=buy&quantity=0.00086";
Call function:
Kupovina($buy_parametri);
First, thanks for you reading. Here is my code
what my json data looks like when viewed in a browser using the url,
{
"$totalResults": 1500,
"$startIndex": 1,
"$itemsPerPage": 3,
"$resources": [
{
"$uuid": "5e7b9312-52e5-4fe1-b3e4-633ca04c9764",
"$httpStatus": "OK",
"$descriptor": "",
"name": "Heinz Tomato Sauce Sachets Qty 200"
},
{
"$uuid": "1a0f9dca-c417-4cff-94d2-99d16438723f",
"$httpStatus": "OK",
"$descriptor": "",
"name": "Heinz Brown Sauce Sachet Qty 200"
},
{
"$uuid": "126fdb17-81ce-41b4-bdba-91d0a170262a",
"$httpStatus": "OK",
"$descriptor": "",
"name": "Heinz English Mustard Sachets Qty 300"
}
]
}
test2.php
<?php
// SAGE URL
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json';
//SAGE USERNAME & PASSWORD
$username = 'test';
$password = 'test';
//initiaize
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$result = curl_exec($ch);
//close curl session / free resources
curl_close($ch);
$json = json_decode($result);
foreach ($json->{'$resources'} as $mydata) {
echo $mydata->name;
};
?>
errors im getting
Notice: Trying to get property of non-object in C:\xampp\htdocs\Sage Json\test2.php on line 29
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Sage Json\test2.php on line 29
I’m not sure what's going wrong, this works if i get the same data from a file but not if I’m retrieving it from at rest server, and if anyone could shed some light I would be grateful
Ajking11 the reason your getting Invalid argument supplied for foreach() is that $json vairiable is overwritten and has no data try somthing like this
<?php
// SAGE URL
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3';
//SAGE USERNAME & PASSWORD
$username = 'test';
$password = 'test';
//initiaize
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$result = curl_exec($ch);
//close curl session / free resources
curl_close($ch);
$xml = simplexml_load_string($result);
$data = json_encode($xml);
$arr = json_decode($data,TRUE);
foreach ($arr['entry'] as $k=>$v){
echo $v; // etc.
}
note that I have removed &format=json and encoded later hope this help let me know how your getting on
you might like to use this code and refine it to your needs
<?php
// SAGE URL
//$url = 'http://computer_2:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&select=name&count=10&format=json';
$url ='http://localhost:5493/sdata/accounts50/GCRM/-/tradingAccounts?select=name&count=10';
//SAGE USERNAME & PASSWORD
$username = 'MANAGER';
$password = '';
//initiaize
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$result = curl_exec($ch);
//close curl session / free resources
curl_close($ch);
$xml = simplexml_load_string($result);
foreach($xml->xpath("//sdata:payload") as $entry) {
// xpath here must be from payload to tradingAccount
$content = $entry->xpath("./crm:tradingAccount");
foreach($content as $c) {
// Make set of children with prefix crm
$nodes = $c->children('crm', true);
echo $nodes->reference . " | " . $nodes->name . " / " . $nodes->openedDate . "<br />\n";
}
}
thanks PHPhil and splash58 for your assistance with the namespace issue
As Artful_dodger say's the json is the problem here is how I would solve it
<?php
// SAGE URL
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json';
//SAGE USERNAME & PASSWORD
$username = 'MANAGER';
$password = '';
//initiaize
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$result = curl_exec($ch);
//close curl session / free resources
curl_close($ch);
// doing a substring as there are 3 weird characters being passed back from IIS in front of the string
$json = json_decode(substr($result, 3, strlen($result)), true);
foreach ($json['$resources'] as $mydata) {
echo $mydata['name'] . "<br>";
}
PHP json_decode function returns an array and not an object, so instead of using
foreach ($json->{'$resources'} as $mydata) {
echo $mydata->name;
};
you should use
foreach ($json['$resources'] as $mydata) {
echo $mydata['name'];
};
UPDATE
your test2.php should look like this:
<?php
// SAGE URL
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json';
//SAGE USERNAME & PASSWORD
$username = 'test';
$password = 'test';
//initiaize
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$result = curl_exec($ch);
//close curl session / free resources
curl_close($ch);
$json = json_decode($result);
$json = (array)$json;
foreach ($json['$resources'] as $mydata) {
echo $mydata->name . "<br>";
};
in your code the major problem was in naming of the JSON attribute $resources: it is named as PHP variable and it causes the problem.
Converting the decoded variable into an array solves the problem as the associative array key is a string.
im really sorry for never getting back to you. so it turn out that when you get data from the sage service, it returned to you in "utf 8 bom".
So before i can display any thing i needed to remove the format by clearing the first three characters with in the array. with the substr() function.
$json_data = substr($json_data, 3);
Thank you all for helping with this problem.