xml-rpc API request client - php

I have to get some account information from xml-rpc API at sippy softswitch.
http://support.sippysoft.com/support/solutions/articles/77553-understanding-authentication,
http://support.sippysoft.com/support/solutions/articles/107367-get-cdrs-of-an-account but I can't construct my code correctly.
The documentation is very limited and I can't understand a lot.
This is my current code:
$urlCdr = "https://portal.mcginc.com/xmlapi/xmlapi";
$post_data = array(
'username'=> $USER,
'password'=> $PASS,
);
$options = array(
CURLOPT_URL => $urlCdr,
CURLOPT_HEADER => true,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false, // for https
CURLOPT_USERPWD => $USER . ":" . $PASS,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($post_data)
);
$ch = curl_init();
curl_setopt_array( $ch, $options );
try {
$raw_response = curl_exec( $ch );
} catch(Exception $ex) {
if ($ch != null) curl_close($ch);
throw new Exception($ex);
}
if ($ch != null) curl_close($ch);
$cdr = "raw response: " . $raw_response;`
It returns nonce realm qop and so on. What should I do after that. Sending this info again to the server?

Please try this:
function Callmethod(){
$url = "https://portal.mcginc.com/xmlapi/xmlapi";
$method = "getAccountCDRs";
$params = array('i_account'=>1);//1 is id account
$post = xmlrpc_encode_request($method, $params);
$ch= curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
curl_setopt($ch, CURLOPT_USERPWD, 'user' . ':' . 'password');
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post );
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error_no = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error_no !=0){
die("CURL Error {$curl_error_no} - {$curl_error}n");
}
if ($response_code != 200){
die("ERROR response code:{$response_code} - {$response}n");
}
return xmlrpc_decode($response);
}

Related

Why does curl_setopt_array break requests and curl_setopt doesn't?

when i try to make a request implementing curl_setopt_array it doesn't work and sophos returns an error:
<?php
try {
$url = 'http://127.0.0.1/index.php';
$fields = array(
'JsonData'=>json_encode(['test' => 'test'])
);
$postvars = http_build_query($fields);
$options = [
CURLOPT_URL => $url,
CURLOPT_POST => count($fields),
CURLOPT_POSTFIELDS => $postvars,
CURLOPT_TIMEOUT => 2,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_FORBID_REUSE => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_DNS_CACHE_TIMEOUT => 10,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HTTPHEADER => array('Connection: close')
];
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
echo 'Finish!!!'
} catch (Exception $e) {
echo $e->getMessage();
}
note: curl_setopt_array return true but, when perform the curl_exec my sophos firewall return error:
Error: Content could not be delivered fue to the folloeing condition: Invalid argument
when i tried to make a request by implementing curl_setopt works correctly and not get a sophos interruption:
<?php
try {
$url = 'http://127.0.0.1/index.php';
$fields = array(
'JsonData'=>json_encode(['test' => 'test'])
);
$postvars = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
curl_exec($ch);
curl_close($ch);
echo 'Finish!!!'
} catch (Exception $e) {
echo $e->getMessage();
}
Why the second works correctly and the first does not work? From my point of view I'm doing the same...

Why does it return null on the stripe API in PHP?

i'm trying to var dump the $response of my code bellow but it returns null and I don't know why...
$token = $_POST['stripeToken'];
$email = $_POST['email'];
$name = $_POST['name'];
if(filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($name) &&
!empty($token)){
$ch = curl_init();
$data = [
'source' => $token,
'description' => $name,
'email' => $email
];
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.stripe.com/v1/customers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => 'sk_test_************************',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_POSTFIELDS => http_build_query($data)
]);
$response = json_decode(curl_exec($ch));
curl_close($ch);
var_dump($response);
die();
}
For info, the $_POST variables are in another file and I verified that are filled with the informations of the form.
Hope someone can help
Thanks in advance !
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/customers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "source=" . $token . "&email=" . $cEmail);
curl_setopt($ch, CURLOPT_USERPWD, 'sk_test_xxxxxxxxxx' . ':' . '');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$obj = json_decode($result);
curl_close($ch);
$customer = $obj->id;
You are welcome :)

PHP cURL body does not work with multiple arrays

I need to send a data via cURL in PHP.
There is my POST request and it works with this example (I can post 1 record).
$postData = array(
"username" => "email#domain.com",
"name" => "Name",
"surname" => "Surname",
'role' => 'user',
"disabled" => "false"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt_array($ch,
array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
// ,CURLOPT_POSTFIELDS => json_encode($postData)
));
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
return stripslashes($response);
curl_close($ch);
But I would like to add records from database into $postData array but then it's stops.
This is my database array example:
$postData = array();
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
array_push($postData, $row);
}
json array looks ok but I can't find an error. Or i can't post multiple data and i need to add everything in a loop?
I find a few flaws in the code. You said that you are posting the code but you have set
CURLOPT_POST => false
Also, you have repeated
CURLOPT_RETURNTRANSFER => TRUE and CURLOPT_RETURNTRANSFER => 1
Also, your API might be designed to accept one record but you are trying to send multiple records. If it is designed to accept 1 record at a time you should make the curl call a function and call it in place of array_push. eg:. curlfunction($row);
function curlfunction($postData){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST , 1);
curl_setopt_array($ch, CURLOPT_HTTPHEADER, array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
curl_close($ch);
return stripslashes($response);
}
and
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
curlfunction($row);
}

PHP request to API endpoint with authorized header

I would like to ask you how to use PHP request to API with authorized header.
API end point example is
Quotation Service : ­ POST https://api.website.com/v4/quotation
Content­type: application/json; charset=utf­8
Accept: application/json
Authorization: hmac $id:$milliSeconds:$signature
{
"serviceType": "CARD",
"specialRequests": [
"ROUNDTRIP"
]}
as you mention, they use hmac as an authorization in a header and request parameters is serviceType, specialRequest
I try using PHPCurl to make a request but i'm not a good on it. Please give me and advice how to use Curl with this API.
edit:
<?php
$customerId = "585a-SAMPLE-980dfe0097"; //by provider
$privKey = "MC4CAQACBQDSk4ghAgMB---SAMPLE---QIDAOPFAgMAk7QIDAMYq"; //by provider
$requestTime = (int)(microtime(true) * 1000);
$signature = hash_hmac('sha256', $requestTime, $privKey);
$headers = array(
"Authorization: hmac $customerId:$requestTime:$signature"
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.samplesite.com/v1/quotations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Regards,
Hmm ... maybe something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/v4/quotation');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
/* if you want to use SSL certificate, otherwise delete this */
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAPATH , '/path/to/certificate');
$auth = hash_hmac('sha1', $id.':'.$milisecond.':'.$signature, <ENCRYPTION KEY>, true);
$headers = array(
'Authorization: '.$auth
);
$params = array(
'serviceType' => 'CARD',
'specialRequests' => array('ROUNDTRIP')
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

I can't log on to a page using curl

I need to log on a page using curl to get a value from the page, but I don't get any result at all.
Have I missed something or is it maybe impossible to implement?
<?
$u = "******************";
$p = "******************";
$token = file_get_contents("https://secure.izettle.com/portal/login");
preg_match('/content=\"(.*?)\" name=\"csrf-token/', $token, $t);
$authenticity_token = $t[1];
echo $authenticity_token;
$fields = array("user[email_address]" => $u, "user[password]" => $p, "authenticity_token" => $authenticity_token );
$ch = curl_init();
//Set curl options
$options = array(
CURLOPT_URL => "https://secure.izettle.com/portal/login",
CURLOPT_COOKIEJAR => "cookie.txt",
CURLOPT_COOKIEFILE => "cookie.txt",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://secure.izettle.com/portal/reports?date=2014-05-21");
$page = curl_exec($ch);
if(!curl_exec($ch)){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo $page.'<br>';
Is it possible for split up the code and try. Not sure it will work. Assumes the second form method is "post"
<?php
$u = "******************";
$p = "******************";
$fields = array("user[email_address]" => $u, "user[password]" => $p);
$ch = curl_init();
//Set curl options
$options = array(
CURLOPT_URL => "https://secure.izettle.com/portal/login",
CURLOPT_COOKIEJAR => "cookie.txt",
CURLOPT_COOKIEFILE => "cookie.txt",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_FOLLOWLOCATION =>true
);
curl_setopt_array($ch, $options);
$res = curl_exec($ch);
unset($ch);
//*****************************************************************************
$qry_str = "?date=2014-05-21";
$ch = curl_init();
// Set query data here with the URL
curl_setopt($ch, CURLOPT_URL, 'secure.izettle.com/portal/…' . $qry_str);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
print_r($content);
?>

Categories