curl instead of file_get_contents with context - php

i would like to replace file_get_contents with curl (sipgate voip account) , but every tryout does not work.
Version mit file_get_contents:
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
'method' => "POST",
'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
'content' => $request
))
);
file_get_contents("https://api.sipgate.net/RPC2", false, $context);
Latest Tryouts with curl
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
'method' => "POST",
'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
'content' => $request
))
);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $context);
curl_exec($curl);
curl_close($curl);
AND
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_exec($curl);
curl_close($curl);
Has someone an idea for me?
Thx, spf

The above named error on both versions
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
'method' => "POST",
'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
'content' => $request
))
);
$data = http_build_query($context);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);
and
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$data = http_build_query($request);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);

Related

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);
}
}

Trustpilot Oauth use token PHP

I retrieve the Oauth token in Trustpilot with this script
function get_accesstoken($tp_username,$tp_password,$api_key,$api_secret)
{
$url = 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken';
$payloadName = array(
'grant_type' => 'password',
'username' => $tp_username,
'password' => $tp_password
);
$payload = http_build_query($payloadName);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$api_key:$api_secret");
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$return = json_decode(curl_exec($curl));
print_r($return);
curl_close($curl);
return $return->access_token;
}
now, how can I retrieve the token, verify is exist and use it?
thanks
$url = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";
$key = "YOUR API KEY";
$secret = "YOUR API SECRET";
$username = 'YOUR LOGIN EMAIL';
$password = 'YOUR LOGIN PASSWORD';
$payload = array(
'grant_type' => 'password',
'username' => $username,
'password' => $password,
);
$authEncodedKeys = base64_encode($key.":".$secret);
$options = array(
'http' => array (
'header' => "Authorization: Basic ".$authEncodedKeys.
"Content-Type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($payload)
)
);
$context = stream_context_create($options);
$results = json_encode(file_get_contents($url, false, $context));
echo $results;
This works for me. Try it.

Adding OneSignal segments to filters array

I am using following PHP script to send OneSignal push notifications to subscribed devices.
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message desde el backend small icon '
);
$fields = array(
'app_id' => "xxxx",
'filters' => array(array("field" => "tag", "key" => "correo", "relation" => "=", "value" => "xxx")),
'data' => array("foo" => "bar"),
'small_icon' =>"ic_push",
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
This script is working fine, and the notifications are sent.
Now I need to add more filters, for example I need to add a filter to two other segments that I have created at the OneSignal dashboard.
The segments are "skateboard" and "administrators".
How can I add these two segment to the filters array?
You need to pass segments name array in fields like you are passing tags.
$content = array(
"en" => 'Notification Message Here..'
);
$heading = array(
"en" => 'Heading goes here..'
);
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXXX',
'contents' => $content,
'headings' => $heading,
'included_segments' => array('SegmentName1','SegmentName2'),
'tags' = array(array("key" => "state", "relation" => "=", "value" => 'Delhi'))
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic XXXXXXXXXXXXXXXXXXX'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;

Onesignal-php to android

I have this php script:
<?php
function sendMessage(){
$content = array(
"en" => 'text message test'
);
$fields = array(
'app_id' => "XXXXXX",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'headings' => array("en" => "Test message!!"),
"isAndroid"=>true,
// 'android_group' => 'Tanks',
// 'android_group_message' => array("en" => "message"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
?>
But is doesn't end notifactions to android users. Is there a example to send pushnotifications from php over OneSignal to android devies?
Well, i did not add my one sognal authentication code ;)

how can i add items in youtubeplaylist?

I am trying to use this code to insert item in youtubeplaylist and its not working as i want ... i think its showing me if playlist contain this id .
please check and let me know what i need to change in code to insert this ID in my playlist .
Here is code :
$option = array(
'part' => 'snippet',
'mine' => 'true',
'key' => 'AIzaSyCJlbT_sqTG2nOUMYU24WzzxXpOelaiYTA',
'access_token' => $_SESSION['info']['access_token'],
'playlistId' => $ID,
'kind' => 'youtube#video',
'videoId' => 'KMGuyGY5gvY',
);
$url = "https://www.googleapis.com/youtube/v3/playlistItems?".http_build_query($option, 'a', '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlheader[0] = "Authorization: Bearer " . $_SESSION['info']['access_token'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
$json_response = curl_exec($curl);
curl_close($curl);
$responseObj = json_decode($json_response);
thanks in advance.

Categories