How to round the degrees OpenWeather APIs - openweathermap

I have a script to extract the time, and for a long time I've been trying to round the degrees from 6.27C ° to 6C °. How can I do this?
<?php
$apiKey = "";
$cityId = "";
$apiUrl = "http://api.openweathermap.org/data/2.5/weather?id=" . $cityId . "&lang=en&units=metric&APPID=" . $apiKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$currentTime = time();
?>
<span class="weather-temp"><?php echo $data->main->temp;?> C°</span>

Related

How to change `curl_multi_init` single thread to multi thread

Ask experts how to change to curl_multi_init multi-threaded?
My idea is to split the $data array into array_chunk($data, 15, true); and then do CURL, but I'm a novice, I don't understand the examples on the Internet, please help
Simulate 100 pieces of data:
function post($url, $data = '', $head = 'application/x-www-form-urlencoded')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:{$head};charset=utf-8;"));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$url = 'http://localhost/test.php';
$res = [];
for ($i=0; $i < 100; $i++) {
$res[$key] = post($url, 'key='.$i);
}
var_dump($res);
The code below, I know it must be wrong, please correct me, thank you!!
$newData = array_chunk($data, 10, true);
foreach ($newData as $k=> $tmp) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/x-www-form-urlencoded;charset=utf-8;"));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$mh = curl_multi_init();
foreach ($tmp as $key => $value) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $tmp);
curl_multi_add_handle($mh, $ch);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
$res[$k] = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
}
There were several issues in your original code, which were fixed in the code above.
Each $ch should be an independent handle, not a common one
curl_multi_getcontent should be called on each $ch handle, you are equivalent to only calling the last $ch of each group.
curl_multi_remove_handle should be called on each $ch handle. It is more appropriate to call it after the previous curl_multi_getcontent.
$newData = array_chunk($data, 10, true);
foreach ($newData as $k => $tmp) {
$mh = curl_multi_init();
$chs = [];
foreach ($tmp as $key => $url) {
$ch = curl_init();
$chs[$key] = $ch;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded;charset=utf-8;'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $tmp);
curl_multi_add_handle($mh, $ch);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($chs as $key => $ch) {
$res[$k][$key] = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
}
var_dump($res);

How to update Products inventory from 3rd party POS system in Magento2?

How to update Products inventory from 3rd party POS system in Magento2?
I need to make a button in POS system when I hit that button the products inventory should be update from POS to Magento2 system.
Here is working code for update qty from Rest API
<?php $adminUrl = 'http://localhost/magento2/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "admin"); //Admin Login details
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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','Content-Length: ' . strlen($data_string)));
$token = curl_exec($ch);
$token= json_decode($token);
$headers = array("Authorization: Bearer $token","Content-Type: application/json");
$skus = array('100001' => 66,'100002' => 99); //Here 100001 and 100002 are SKU and 66 and 99 are Qty
foreach ($skus as $sku => $stock) {
$requestUrl='http://localhost/magento2/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array("qty" => $stock);
$productData = json_encode(array('stockItem' => $sampleProductData));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
unset($productData);
unset($sampleProductData);
}

Ebay API: Get Item Purchase History

Basically I need to get the same information that is presented here
http://offer.ebay.com/ws/eBayISAPI.dll?ViewBidsLogin&item=221864583914&rt=nc&_trksid=p2047675.l2564
According to the documentation, GetItemTransactions is the method I'm searching for. I tried that and it gives no transactions.
Here is the code if you'd like to test it yourself(curl should be enabled)
$url = "https://api.ebay.com/ws/api.dll";
$token = "AgAAAA**AQAAAA**aAAAAA**D55JWA**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6ABlIWoAZeApg2dj6x9nY+seQ**23sDAA**AAMAAA**efeUquPbnQ2MiXisn2kcSIvJ3MOhnzuRf7V5BmJdhMLNVX11yxak3P3jIoDV58s7VAFRcYfTnaltMr3CNRBpeVFAKry+t+jTfuXAaKRfUN2z4LjNngQH5/QhJTrDsVTTEinEizj4UXnpNLXWYErZ4sEn9gGcG6jALdKbLam36m9EeBbF9ZDTUivd8MkQYXOAa3MWl3+vau06gbd9Dm3sXiMB46ainm8EvxAm+ZUEGtxCNRHTDdsuMOTzmC0Jc5IYisf2kWMSyjaFG3bruEo1WLAYsSGuixOwLtVEAZkc5MVbSoHtfv9q4HWHv0o0hOgJP/dN7eyYRhC6uclvs+fk4NyyOh3+6hSKYEcyNXdFm+9ZJmZdCieg4PtEmyrmSy2YV5M/Upqq53D9+TSfBHchfGZDCrFw3fY3RHnE1gZKnV3gaOb6lAbhDV79QDkM4Qyi3wfEQE0FzPvzDzUuADy1nEtPSRa2Koz1bR57Lvt5dc/vh0z0JLvyNVEs6wZNfDcj5mwgXvryQMMMBIOAgY/w3gEHiuyw/Z7iIV+y9h4FMHIaroeAaBpHa5JXbojkCph5Ej8KUOesjbUstZXEjBD+XdEDRSK5ThR2/T6+9N5eU1uj+7PuhoHllZhH6AKpo5tDlAzkTfU5YeK7vtV8Mb4ByHNgAA08nizMM1bc35YMmf1LS7D6gV/pNNT2t5cDXsoP6kWtcbsmwIW+XaBj+A2J7a0VxWmXvuDQI3q7+HpLgA1q35Mke5pACVE1OlOvXzOt";
function GetItemTransactions(){
global $url, $token;
$post = '<?xml version="1.0" encoding="utf-8"?>
<GetItemTransactionsRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<ItemID>152343698747</ItemID>
<RequesterCredentials>
<eBayAuthToken>'.$token.'</eBayAuthToken>
</RequesterCredentials>
</GetItemTransactionsRequest>';
$headers = array("X-EBAY-API-COMPATIBILITY-LEVEL: 967",
"X-EBAY-API-CALL-NAME: GetItemTransactions",
"X-EBAY-API-SITEID: 0",
"Content-Type: text/xml");
$result = request($url, $post, $headers);
return json_decode(json_encode(simplexml_load_string($result)), true);
}
function request($url, $post, $headers) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if($post){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo "<pre>";
print_r(GetItemTransactions());
echo "</pre>";

shorte.st new api convert to php curl

shorte.st is a url shorten service.
recently they changed the api as following:
curl commandline
curl H "public-api-token: fakekey" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url
response:
{"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
How to change it into php curl version?
function shst($url){
$apiurl="https://api.shorte.st/v1/data/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('public-api-token: fakekey','X-HTTP-Method-Override: PUT'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "urlToShorten=".$url);
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
$ret=$obj->{'shortenedUrl'};
return $ret;
}
The sample they provided used the wrong url. should be https not http
This works:
function shst($url){
$key="";//your key
$curl_url = "https://api.shorte.st/s/".$key."/".$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$array = json_decode($result);
$shortest = $array->shortenedUrl;
return $shortest;
}

How to upload file via cURL in kohana 3?

I try
$request = Request::factory($url)->method(Request::POST)->post('xml', '#' . $filepath);
echo $request->execute();
but print_r($_FILES); in destination script returns empty array.
Version of Kohana is 3.2.0 stable
What I want is a simple analog of
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => '#' . $filepath));
$res = curl_exec($ch);
echo $res;
Try:
$request_url = 'your_url_here';
$post_params['name'] = urlencode('Test User');
$post_params['file'] = ‘#’.'demo/testfile.txt’;
$post_params['submit'] = urlencode('submit');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
curl_close($ch);
Now it's possible to do that with Kohana 3.3 using Request#files() method.
Edit: actually, it's not part of official release, so before 3.4 you probably won't be able to do it.