How to sending many POST requests with cURL faster - api

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.

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.

PHP cURL as DELETE using POSTFIELDS

In my case the user is logged in and would like to delete his account.
Therfore he triggers a POST form, revalidating the process with a confirmation of his password.
Now I would like to send a cURL DELETE to the api/server and at the same time deliver the confirmed password as well.
My approach is:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://www.myurl.com/v1/users",
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode(['cnfpwd' => $_POST['cnfpwd']]), //confirmed password
CURLOPT_HTTPHEADER => array('X-API-KEY: '$apiKey),
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
//For testing purposes only
print_r($response);
print_r($data);
If this approach is correct, how would I get the POSTFIELDS on the API/server side?
As this is a DELETE request, $_GET, $_POST, $_REQUEST are empty.
EDIT:
The api code for testing purposes:
<?php
if($_SERVER['REQUEST_METHOD'] == "DELETE"){
echo"POST: ";print_r($_POST);
}
?>
The result is:
POST: Array ( )
You should be able to get the body of the request with
$data = file_get_contents('php://input');
My final solution is:
$parameters = file_get_contents("php://input");
$parameters = json_decode($parameters, true);
#Paolos Solution is correct, I just needed to add the second line in order to access the parameters as an Array.

Coinimp http api withdraw

I'm trying to write the whole for coinimp a payout for the api v2.0. Unfortunately, I always get back only one empty issue.
maybe one of you can help me and tell you what the problem is here.
$fields = array(
"site-key" => 'c085da9be5ba47309d3805b3fe0e0e66adcda4f6f5041e8e6d21d6bd2abc60ce',
"user" => $this->user,
"amount" => $this->KontoExtern
);
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'http://www.coinimp.com/api/v2/user/withdraw');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$headers = array();
$headers[] = self::X_API_ID;
$headers[] = self::X_API_KEY;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ausgabe = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo 'Ausgabe:'.$ausgabe.'<br>';
curl_close ($ch);
$aus = json_decode($ausgabe);
return $aus;
can someone help me here and say where the problem lies?
the coinIMP API documentation realy is a mess i found this on stackoverflow and it works
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.coinimp.com/api/v2/user/balance?site-key=WEBSITE-ID&user=USER-ID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"x-api-id: PUBLIC-ID",
"x-api-key: SECRET-ID"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
this code is easy to edit to make it work for you !!
change CURLOPT_URL value to: http://www.coinimp.com/api/v2/user/withdraw
change CURLOPT_CUSTOMREQUEST => "GET" to "POST"
Add CURLOPT_POST => 2 to the curl_setopt_array
Add CURLOPT_POSTFIELDS =>""site-key=value&user=value&amount=value
i know this isn't a direct answer to your qluestion but anyway it works :)
note: this will withdrawl Webchain, i haven't tryt Monero jet

CURL is not returning correct response when json data is passed through variable

$data is the variable holding json string {"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}.
as you can see in the first line below. I have encoded array into json using json_encode()
$data = json_encode(array("clientId"=>"MyClientID","clientSecret"=>"MyClientSecret","script"=> $this->input->post("script",true),"stdin"=>$this->input->post("stdin",true),"language"=>$this->input->post("language",true),"versionIndex"=>$this->input->post("versionIndex",true)));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.jdoodle.com/v1/execute",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array("cache-control: no-cache","content-type: application/json"),
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
else {
echo $response;
}
jdoodle is not returning correct $response instead returning the statements that were given to execute. But if I replace $data by actual json string {"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"} at CURLOPT_POSTFIELDS => $data then jdoodle is returning correct $response.
Then, simply
json_encode(array("clientId"=>"MyClientID","clientSecret"=>"MyClientSecret","script"=> $this->input->post("script",true),"stdin"=>$this->input->post("stdin",true),"language"=>$this->input->post("language",true),"versionIndex"=>$this->input->post("versionIndex",true)))
is not producing the $data string that you expect and that gives correct response:
'{"clientId":"MyClientID","clientSecret":"MyClientSecret","script":"<?php\n echo \"Welcome to GLB Coding Club\";\n?>\n","stdin":"","language":"php","versionIndex":"2"}'
Just
echo $data;
exit(0);
after the first line and check what is wrong when forming the $data string
I simply edited $this->input->post("script",true) to html_entity_decode($this->input->post("script",true)).

Bigcommerce php api returning null on initial app install

I'm trying to do initial install of a BigCommerce draft app for oAuth access to api using the official Bigcommerce php library.
For some reason no matter what I do I get null back from a post to BigCommerce api. Can anyone see anything that could be causing this?
Thanks for any advice
<?php
require_once ('vendor/autoload.php');
use Bigcommerce\Api\Connection as Connection;
use Bigcommerce\Api\Client as Bigcommerce;
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$client_id = "*******";
$client_secret = "*******";
if (count($_GET)) {
$code = $_GET['code'];
$context = $_GET['context'];
$scope = $_GET['scope'];
$url = "https://login.bigcommerce.com/oauth2/token";
$connection = new Connection();
$connection->useUrlencoded();
$connection->verifyPeer(false);
$response = $connection->post($url, array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => "https://www.*******.com/bc-auth.php",
"grant_type" => "authorization_code",
"code" => $code,
"scope" => $scope,
"context" => $context));
if (is_null($response)) {
print "Last Error:\n";
print "<pre>";
print_r(Bigcommerce::getLastError());
print "</pre>";
exit();
}
$token = $response->access_token;
die($token); // this is just for testing
}
Try making your own POST request via cURL, papi..
// Prepare POST Data:
$postFields = array(
'client_id' => urlencode($client_id),
'client_secret' => urlencode($client_secret),
'code' => urlencode($code),
'scope' => urlencode($scope),
'grant_type' => urlencode('authorization_code'),
'redirect_uri' => urlencode("https://www.*******.com/bc-auth.php"),
'context' => urlencode($context)
);
// Format data for Post:
$fields_string = '';
foreach($postFields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$postFields = $fields_string; // Single URL string of post fields
unset($fields_string);
// ** Curl Config **//
$ch = curl_init(); //open connection
curl_setopt($ch, CURLOPT_URL, "https://login.bigcommerce.com/oauth2/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch)); // execute post! & assign response
curl_close($ch); // close connection
Removing and inserting it instead of this code:
$connection = new Connection();
$connection->useUrlencoded();
$connection->verifyPeer(false);
$response = $connection->post($url, array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => "https://www.*******.com/bc-auth.php",
"grant_type" => "authorization_code",
"code" => $code,
"scope" => $scope,
"context" => $context));