/    Sign up×
Community /Pin to ProfileBookmark

How to combine WHMCS CURL API to send one request?

I’m trying to build an order page where customer enters his details and than the code gets the detail place the order and redirects him to invoicing page .

It is working but it is too slow

I’m trying to get data through api with json response. My code is working too slow. Is it possible to combine them to speed up the code?

“`
`if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$fname = $_POST[‘firstname’];
$lname = $_POST[‘lastname’];
$umail = $_POST[’email’];
$uadd = $_POST[‘address’];
$ucity = $_POST[‘city’];
$ustate = $_POST[‘state’];
$upcode = $_POST[‘postcode’];
$uctry = $_POST[‘country’];
$uphn = $_POST[‘phonenumber’];
$upass = $_POST[‘password’];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘URL+++++++++++++++’);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
http_build_query(
array(
‘action’ => ‘AddClient’,
‘username’ => ‘+++++++++++++++’,
‘password’ => ‘+++++++++++++++++’,
‘firstname’ => $fname,
‘lastname’ => $lname,
’email’ => $umail,
‘address1’ => $uadd,
‘city’ => $ucity,
‘state’ => $ustate,
‘postcode’ => $upcode,
‘country’ => $uctry,
‘phonenumber’ => $uphn,
‘password2’ => $upass,
‘responsetype’ => ‘json’,
)
)
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$aduresponse = curl_exec($ch);
curl_close($ch);
$useraddresponse = json_decode($aduresponse, true);

$result = $useraddresponse[‘result’];
$clientid = $useraddresponse[‘clientid’];

if ($result == “error”) {
echo $result[‘message’];
}

if (isset($result)) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘+++++++++++++++++++++++++’);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
http_build_query(
array(
‘action’ => ‘AddOrder’,
‘username’ => ‘++++++++++++++++++++++’,
‘password’ => ‘++++++++++++++++++++++++++’,
‘clientid’ => $clientid,
‘pid’ => 65,
‘domain’ => $orderdomain,
‘billingcycle’ => ‘annually’,
‘domaintype’ => ‘register’,
‘regperiod’ => 1,
‘dnsmanagement’ => 0,
‘nameserver1’ => ‘ns1.domain.com’,
‘nameserver2’ => ‘ns2.domain.com’,
‘paymentmethod’ => ‘mailin’,
‘responsetype’ => ‘json’,
)
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$orderres = curl_exec($ch);
curl_close($ch);
$orderaddresponse = json_decode($orderres, true);
$orderresult = $orderaddresponse[‘result’];
if ($orderresult == “error”) {
echo $orderresult[‘message’];
}
}

if ($orderresult == “success”) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘++++++++++++++++++++++++++++’);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
http_build_query(
array(
‘action’ => ‘CreateSsoToken’,
‘username’ => ‘+++++++++++++++++++++++++++’,
‘password’ => ‘+++++++++++++++++++++++++’,
‘client_id’ => $clientid,
‘destination’ => ‘clientarea:invoices’,
‘responsetype’ => ‘json’,
)
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$SSOresponse = curl_exec($ch);
curl_close($ch);

$ssoresult = json_decode($SSOresponse, true);
$ssourl = $ssoresult[‘redirect_url’];
$ssoresultstatus = $ssoresult[‘result’];
}
if ($ssoresultstatus == “error”) {
echo $ssoresultstatus[‘message’];
}

if ($ssoresultstatus == “success”) {
header(‘Location:’ . $ssourl);
}`
“`

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 29.2022 — First thing to do is determine where the delay(s) is. E.g., if the delay is in one of the two curl_exec() calls, then you may not be able to do much, since it's that external API's problem (or your internet connection?).

A simple way to gather some stats:
[code=php]
$start = microtime(true);
// one or more lines of code you want to time
$time = round(microtime(true) - $start, 6);
// you can then do something like error_log("Such-and-such took: $time seconds");
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 29.2022 — PS: As far as combining the API requests, probably not -- unless they provide a separate endpoint specifically for that. There is a little duplication in your code that could probably be eliminated by just creating one cURL handle and then only needing to change its URL and the post fields that change from one request to the other, but that's probably in the millisecond range, most likely -- nothing compared to whatever the http response times are.
Copy linkTweet thisAlerts:
@slake8authorMay 30.2022 — Thank you for the response.

Just figured out my internet connection was slow.


Since I am very new to php and programming

Can you please tell me if my code is correct?
Copy linkTweet thisAlerts:
@NogDogMay 30.2022 — I don't see anything obviously wrong, but I might re-factor it to use a custom function that handles the repetitive cURL stuff, in the name of keeping the code DRY (Don't Repeat Yourself). Along with that, no real need to copy the $_POST array into a set of variables. :)
[code=php]
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$aduresponse = curl(
'URL+++++++++++++++',
array(
'action' => 'AddClient',
'username' => '+++++++++++++++',
'password' => '+++++++++++++++++',
'firstname' => $_POST['firstname'],
'lastname' => $_POST['lastname'],
'email' => $_POST['email'],
'address1' => $_POST['address'],
'city' => $_POST['city'],
'state' => $_POST['state'],
'postcode' => $_POST['postcode'],
'country' => $_POST['country'],
'phonenumber' => $_POST['phonenumber'],
'password2' => $_POST['password'],
'responsetype' => 'json'
)
);
$useraddresponse = json_decode($aduresponse, true);
$result = $useraddresponse['result'];
$clientid = $useraddresponse['clientid'];

if ($result == "error") {
echo $result['message'];
}

if (isset($result)) { // wonder if this should be if(!empty($result)){} ???
$orderres = curl(
'OTHERURL+++++++++++',
array(
'action' => 'AddOrder',
'username' => '++++++++++++++++++++++',
'password' => '++++++++++++++++++++++++++',
'clientid' => $clientid,
'pid' => 65,
'domain' => $orderdomain,
'billingcycle' => 'annually',
'domaintype' => 'register',
'regperiod' => 1,
'dnsmanagement' => 0,
'nameserver1' => 'ns1.domain.com',
'nameserver2' => 'ns2.domain.com',
'paymentmethod' => 'mailin',
'responsetype' => 'json',
)
);
$orderaddresponse = json_decode($orderres, true);
$orderresult = $orderaddresponse['result'];
if ($orderresult == "error") {
echo $orderresult['message'];
}
}

if ($orderresult == "success") {
$SSOresponse = curl(
'URL+++++++++++++++',
array(
'action' => 'CreateSsoToken',
'username' => '+++++++++++++++++++++++++++',
'password' => '+++++++++++++++++++++++++',
'client_id' => $clientid,
'destination' => 'clientarea:invoices',
'responsetype' => 'json'
)
);
$ssoresult = json_decode($SSOresponse, true);
$ssourl = $ssoresult['redirect_url'];
$ssoresultstatus = $ssoresult['result'];
}
if ($ssoresultstatus == "error") {
echo $ssoresultstatus['message'];
}

if ($ssoresultstatus == "success") {
header('Location:' . $ssourl);
}
}

/**
* Execute a cURL POST request
*
* @param String $url
* @param Array $data
* @return String
*/
function curl($url, Array $data) {
static $ch; // so we can re-use it on each call
// only needs to create it once
if(empty($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
return curl_exec($ch);
}
[/code]

Untested, so no guarantees. ;)
Copy linkTweet thisAlerts:
@slake8authorMay 30.2022 — Thank you for the amazing tips

I will definitely work on functions from now on
×

Success!

Help @slake8 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...