how to call cs cart api for creating orders? - php

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

Related

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"
),
));

RESTful webservice Moodle plugin Authentication fails

I am using moodle rest plugins for mobile web-services and from this tutorial I have enabled rest API and other related services as per the documentation when I try to test the web service then I got following error
{"exception":"moodle_exception","errorcode":"noauthheader","message":"No Authorization header found in request sent to Moodle"}
I have the cross check the moodle token even try may new tokens but no luck
here is me test php code to check the web-services:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "mydomain.com/webservice/restful/server.php/core_course_get_courses",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"options": {"ids":[2]}}',
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: {4838e6771cdbfd1eefbf37b3839587d9}",
"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;
}
please let me know, what i am doing wronge
thanks

PHP cURL with API Key and Secret (Postman)

I'm working with an API which is made by a classmate. I used Postman to generate the PHP cURL for the connection, with Authorization Basic. This works perfectly. Now, I want to get rid of the Authorization Basic and use my own API Key & Secret (password). Are there any good ways to do this?
Thanks in advance!
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "Some link here",
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(
"authorization: Basic //something here",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response,true);
}

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

How to write json post in this given sample CURL module

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

Categories