How to write json post in this given sample CURL module - php

i am trying to implement interswitchng.com api. following code is generated from postman's generate code features. When i try to change following status value with dynamic value using php then it shows some error. i think i do some mistake in CURLOPT_POSTFIELDS. So can you suggest me that how can i write this array in CURLOPT_POSTFIELDS ?
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.interswitchng.com/api/v1/quickteller/fundsTransfer",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"mac\": \"8285b03723393d92c30b31474689bc835e448a34345324339e1aef2d0a1a03a438f934a177e0515459e0b6eb6c0c7f3d9a9e26ec3e4447a664343455\",\r\n \"beneficiary\": {\r\n \"lastname\": \"Anari\",\r\n \"othernames\": \"Sammy\"\r\n },\r\n \"initiatingEntityCode\": \"XXT\",\r\n \"initiation\": {\r\n \"amount\": \"100000\",\r\n \"channel\": \"7\",\r\n \"currencyCode\": \"566\",\r\n \"paymentMethodCode\": \"CA\"\r\n },\r\n \"sender\": {\r\n \"email\": \"simon.mokhele#hellogroup.co.za\",\r\n \"lastname\": \"Testing\",\r\n \"othernames\": \"Test\",\r\n \"phone\": \"0732246413\"\r\n },\r\n \"termination\": {\r\n \"accountReceivable\": {\r\n \"accountNumber\": \"0014261063\",\r\n \"accountType\": \"10\"\r\n },\r\n \"amount\": \"100000\",\r\n \"countryCode\": \"NG\",\r\n \"currencyCode\": \"566\",\r\n \"entityCode\": \"058\",\r\n \"paymentMethodCode\": \"AC\"\r\n },\r\n \"transferCode\": \"27205265080\"\r\n}",
CURLOPT_HTTPHEADER => array(
"authorization: InterswitchAuth SUtJQUI2MzQ0OTlFQjk434adsfdfsdf23zNjkzQTcyODRDQ0EzNDY=",
"cache-control: no-cache",
"content-type: text/html",
"nonce: 6fb704016f989a763453456347777",
"signature: +GWrb0434543345gg=",
"signaturemethod: SHA1",
"terminalid: 2vXP0434",
"timestamp: 3343424"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
If i try to use following sentence, then it does not seem proper (i think).
CURLOPT_POSTFIELDS => json_encode($data['postdata']),
Above $data has been send using following code :
$postdata=array( "mac"=>"$maccode", "beneficiary"=>array("lastname"=>"$receiverlastname","othernames"=>"$receiverothernames"), "initiatingEntityCode"=>"$InstitutionCode", "initiation"=>array( "amount"=>"$initiation_amount", "channel"=>"7", "initiation_currencyCode"=>"556", "initiation_paymentMethodCode"=>"$initiation_paymentMethodCode" ), "sender"=>array("email"=>"$senderemail","lastname"=>"$senderlastname","othernames"=>"$senderothername","phone"=>"$senderphone"), "termination"=> array(
"accountReceivable"=>array("accountNumber"=>"0014261063","accountType"=>"10"),
"amount"=>"$to_amount",
"countryCode"=>"$to_countryCode",
"currencyCode"=>"$to_currencyCode",
"entityCode"=>"$to_entityCode",
"paymentMethodCode"=>"$to_paymentMethodCode"
), "transferCode"=>"$transferCode" );
$data=array("url"=>"$url", "header"=>array("Authorization"=>"InterswitchAuth $Authorization","Signature"=>"$signature","Nonce"=>"$Nonce","Timestamp"=>"$timestamp","SignatureMethod"=>"SHA1","TerminalId"=>"2vXP0434"), "postdata"=>$postdata );

Related

Microsoft Translator Text API CUrl PHP

I have to translate some information of my database to make PDF. I make a PHP function called "translate" that receive the strinf to be trtanslated by parameter. I have this API cURL code:
function translate($text)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://microsoft-translator-text.p.rapidapi.com/translate?to=fr&api-version=3.0&from=pt&profanityAction=NoAction&textType=plain",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "[\r
{\r
\"Text\": \"I would really like to drive your car around the block a few times.\"\r
}\r
]",
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: microsoft-translator-text.p.rapidapi.com",
"X-RapidAPI-Key: KEY",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$json = json_decode($response);
echo $json[0]->translations[0]->text;
}
}
I want to replace:
CURLOPT_POSTFIELDS => "[\r
{\r
\"Text\": \"I would really like to drive your car around the block a few times.\"\r
}\r
]",
with:
$text
But i donĀ“t know how. Can you help me?

Determine the file type downloaded by cURL

I have this code,
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://example.com/v1/items/id/preview",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer bearer-string",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
file_put_contents('file.jpeg', print_r($response, true));
}
From that code I can tell that the response is jpeg that's why I assigned file.jpeg as filename.
Now my problem, what if the response is not jpeg?
I could not find any function from https://www.php.net/manual/en/ref.filesystem.php that will dynamically determine what type of file is in response.
Any Idea about this.
You can use curl_getinfo to get content-type of what curl got
$mime = curl_getinfo(response , CURLINFO_CONTENT_TYPE);
and then simply check if $mime equals to 'image/jpeg'

how to call cs cart api for creating orders?

I am creating api function in my one of my cs cart website and where submit the order through api function.
This is for a new windows server, running MySQL 5, PHP 5.6 and IIS. and I am trying, on postman, to validate what's the data has been transfer.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://my-testwebsite.com/api.php?_d=orders&ajax_custom=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"user_id\": \"4\",\r\n \"shipping_id\": \"0\",\r\n \"payment_id\": \"6\",\r\n \"products\": {\r\n \"1000323256\": {\r\n \"product_id\": \"1002\",\r\n \"amount\": \"100\"\r\n },\r\n \"103101663\": {\r\n \"product_id\": \"1002\",\r\n \"amount\":\"100\"\r\n }\r\n }\r\n}",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic dmluY2VudC5hcm9ja2lhcmFqQGZ1amlmaWxtLmNvbTplUUE3NzdmN2RrSUR0ODVieVoyM1R0NDYxVzMwRGNTMQ==",
"Content-Type: application/json",
"Postman-Token: 050a8bff-d6da-4a0d-b6e8-4cb01bd2c112",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
Please follow CS-Cart Docs > Developer Guide > REST API > Supported Entities > Orders > Create an Order

Acumatica REST Contract-Based API and PHP Curl As ERROR Has Occured to Create Data

I've been trying to create customer in Acumatica contract-based REST API following TIM RODMAN method and a little tweak of code
and all I'm getting is an error
{"message":"An error has occurred."}
I have tried to get data (GET all data) has been successful, but when I try to create new data customer, purchase order or else i got an error appears as above
Note: The same create in Postman didn't work, but start from login, get data, and logout work fine.
See the code below for my latest version of simplified code
function login_acumatica($cookie_jar, $curl){
// Login to Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaMMI/entity/auth/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"name\": \"admin\",\r\n \"password\": \"1112345\",\r\n \"company\": \"DUMMY USER\"\r\n}",
CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
function logout_acumatica($cookie_jar, $curl){
// Logout of Acumatica REST API
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaMMI/entity/auth/logout",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEFILE => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
// Close Connection
curl_close($curl);
// Remove Cookie Jar
unlink($cookie_jar) or die("Can't unlink $cookie_jar");
}
switch ($_GET['query']) {
case 'create_customer':
// Add Cookie Jar
$cookie_jar = tempnam('/tmp','cookie.txt');
// Initiate Connection
$curl = curl_init();
login_acumatica($cookie_jar, $curl);
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaMMI/entity/Default/6.00.001/CUstomer",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n\t\"CustomerID\": {\"value\":\"C-00023\"},\n\t\"CustomerName\": {\"value\":\"Cust Test 1\"}\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
logout_acumatica($cookie_jar, $curl);
break;
default:
echo dirname(__FILE__) . '/cookie.txt';
break;
}
sorry for my bad english. thanks in advance
Unless you're using OAuth (which you are not), Acumatica requires cookies for the authentication to work. Postman handles cookies autimatically. As far as I can see, you don't transfer cookies between login call and subsequent calls, which is why your setup doesn't work.
Try something like this from Tim Rodman
// Add Cookie Jar
$cookie_jar = tempnam('/tmp','cookie');
// Initiate Connection
$curl = curl_init();
// Login to Acumatica REST API
echo "START <br><br>";
curl_setopt_array($curl, array(
CURLOPT_URL => "http://111.11.111.11/AcumaticaIII/entity/auth/login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_COOKIESESSION => 1,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"name\": \"admin\",\r\n \"password\": \"123\",\r\n }",
CURLOPT_HTTPHEADER => array( "cache-control: no-cache", "content-type: application/json", "postman-token: e0a0ff40-8d46-4c5f-106b-960ad1aafba8"
),
));

Api request working with postman and resteasy but not working in php curl

This is the code generated by postman
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://smsgateway.me/api/v4/message/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "[\r\n {\r\n \"phone_number\": \"40404\",\r\n \"message\": \"whois #akborodo\",\r\n \"device_id\": 82531\r\n }\r\n \r\n ]",
CURLOPT_HTTPHEADER => array(
"authorization: <api-key>",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
when i try it it display HTTP ERROR 500 int the browser, but it practically worked well in postman

Categories