How to create Sales order attachment in Zoho Books with using Sales Order attachment API? - php-curl

I am trying to create Sales Order attachment in Zoho Books using Sales Order attachment API but it gives me error like "[code] => 33003 [message] => Receipt not attached".
If I try API with postman it works.
Below is my API call copied from postman.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL =>
"https://books.zoho.com/api/v3/salesorders/**********/attachment?
organization_id=********",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-
Disposition: form-data; name=\"attachment\";
filename=\"D:\\wamp\\www\\*****\\01-30-18 E01301811384413.pdf\"\r\nContent-
Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--
",
CURLOPT_HTTPHEADER => array(
"Authorization: *********************",
"Cache-Control: no-cache",
"Content-Type: application/json",
"Postman-Token: e1141774-32e0-09f1-059b-6d2ee0c44095",
"content-type: multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Any help is appreciated.

The code generated by postman is not working for any file upload API's.
Please try this below php code snippet for uploading file to Zoho Books.
<?php
$file_name_with_full_path = '/Users/Admin/Desktop/1.png';
$request_url = 'https://books.zoho.com/api/v3/salesorders/******/attachment';
if (function_exists('curl_file_create')) { // php 5.6+
$cFile = curl_file_create($file_name_with_full_path);
} else { //
$cFile = '#' . realpath($file_name_with_full_path);
}
$post = array(
'authtoken' => '*******',
'organization_id' => '*******',
'attachment'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($ch);
curl_close ($ch);
print_r($r);
?>

Related

Trouble Connecting with BigCommerce API

I'm using the following to try and test a GET request on the BigCommerce API locally, but I'm getting a "Failed to connect to api.bigcommerce.com port 443: Connection refused". Any help would be appreciated. Thanks in advance.
$api_url = 'https://api.bigcommerce.com/stores/STORE-HASH/v3/customers';
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"X-Auth-Client: xxxxxxxx",
"X-Auth-Token:xxxxxxxx"
);
$chbig = curl_init();
curl_setopt($chbig, CURLOPT_URL, $api_url);
curl_setopt($chbig, CURLOPT_TIMEOUT, 60);
curl_setopt($chbig, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $chbig, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $chbig, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $chbig, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($chbig); //execute request
$http_status = (string) curl_getinfo($chbig, CURLINFO_HTTP_CODE);
if(curl_errno($chbig))
{
echo 'Curl error: ' . curl_error($chbig);
}
curl_close($chbig);
echo "<pre>";
print_r($http_status);
print_r($result);```
I really like to use Postman for testing out my API requests. You may want to download that as well, if you have not already.
Here is the code that I am using to connect to my customers
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.bigcommerce.com/stores/{STORE_HASH}/v3/customers",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"X-Auth-Token: {TOKEN}",
"X-Auth-Client:{CLIENT_ID}"
),
));
$response = curl_exec($curl); // makes the request
curl_close($curl);
echo $response;
You may be having issues due to the Accept and/or Content-type headers. I would remove those headers and only use the 'X-Auth-Token' and 'X-Auth-Client' headers for GET requests. Good luck!

Coinimp http api withdraw

I'm trying to write the whole for coinimp a payout for the api v2.0. Unfortunately, I always get back only one empty issue.
maybe one of you can help me and tell you what the problem is here.
$fields = array(
"site-key" => 'c085da9be5ba47309d3805b3fe0e0e66adcda4f6f5041e8e6d21d6bd2abc60ce',
"user" => $this->user,
"amount" => $this->KontoExtern
);
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'http://www.coinimp.com/api/v2/user/withdraw');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$headers = array();
$headers[] = self::X_API_ID;
$headers[] = self::X_API_KEY;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ausgabe = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo 'Ausgabe:'.$ausgabe.'<br>';
curl_close ($ch);
$aus = json_decode($ausgabe);
return $aus;
can someone help me here and say where the problem lies?
the coinIMP API documentation realy is a mess i found this on stackoverflow and it works
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.coinimp.com/api/v2/user/balance?site-key=WEBSITE-ID&user=USER-ID",
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(
"cache-control: no-cache",
"x-api-id: PUBLIC-ID",
"x-api-key: SECRET-ID"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
this code is easy to edit to make it work for you !!
change CURLOPT_URL value to: http://www.coinimp.com/api/v2/user/withdraw
change CURLOPT_CUSTOMREQUEST => "GET" to "POST"
Add CURLOPT_POST => 2 to the curl_setopt_array
Add CURLOPT_POSTFIELDS =>""site-key=value&user=value&amount=value
i know this isn't a direct answer to your qluestion but anyway it works :)
note: this will withdrawl Webchain, i haven't tryt Monero jet

areto api payment gateway

I am trying to implement the Areto API payment gateway, but it returns response with authentication error such as :
Authentication failed - invalid ID/Session/IP combination.
I am try to first time implement (this type) payment gateway.
how to implement Areto API payment gateway.
Just have use the code like this:
<?php
$url = 'https://pay.aretosystems.com/api/sale/v1';
$api_session = 'ZXUKwKG97WCEdyYBZxnBgFOutVj5qdO7yWaYxT5T9CAbPMn4tamzUpH0HgtdIn6b';
$api_id = '6mK+KXQ5dadlu+4qnxpY3jtH/Hg56AgMhhpOMWXP0/TCmbE6OhXZkG/QRjE/dNjQ';
$transaction = array(
"Authentication" => array(
"Id" => $api_id,
"Session" => $api_session
),
"Order" => array(
"OrderId" => "TESTSALE",
"Amount" => "0.01",
"CurrencyCode" => "EUR"
),
"CardDetails" => array(
"CCVC" => "123",
"CCExpiryMonth" => "01",
"CCExpiryYear" => "2017",
"CCName" => "Wdp",
"CCSurname" => "Tech",
"CCNumber" => '4200000000000000',
"CCType" => "VISA",
"CCAdress" => "542, surya nagar",
"ClientCity" => "Jaipur",
"ClientCountryCode" => "US",
"ClientZip" => "302017",
"ClientEmail" => "wdpjaipur#gmail.com",
"ClientExternalIdentifier" => "MytestUserName1",
"ClientIP" => "162.158.50.205",
"ClientPhone" => "1234567890"
)
);
$data_string = json_encode($transaction);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$http_status . PHP_EOL;
$responceData = json_decode($response, TRUE);
echo '<pre>';
print_r($responceData);die;
?>
This part of the API is not very well documented, but after we found this question we were finally able to find a way to solve the problem:
The key is to have all the properties you want to send concatenated into a string divided by & characters and send them as plain text via POST.
If you want to try it out in Postman:
1) enter the properties as url parameters
2) copy the string from the url
3) and paste it in the request body, setting the type to plain text.
error_reporting(1);
$url = 'https://pay.aretosystems.com/api/sale/v1';
$api_session = 'ZXUKwKG97WCEdyYBZxnBgFOutVj5qdO7yWaYxT5T9CAbPMn4tamzUpH0HgtdIn6b';
$api_id = '6mK+KXQ5dadlu+4qnxpY3jtH/Hg56AgMhhpOMWXP0/TCmbE6OhXZkG/QRjE/dNjQ';
$clientIP = $_SERVER['REMOTE_ADDR'];
$request_arr = array(
'Id'=>$api_id,
'Session'=>$api_session,
'OrderId'=>time(),
'Amount'=>'4.0',
'CurrencyCode'=>'EUR',
'CCVC'=>'123',
'CCExpiryMonth'=>'12',
'CCExpiryYear'=>'2017',
'CCName'=>'wdp',
'CCSurname'=>'technologies',
'CCNumber'=>'4200000000000000',
'CCType'=>'VISA',
'CCAddress'=>'surya nagar',
'ClientCity'=>'Ajmer',
'ClientCountryCode'=>'IN',
'ClientZip'=>'302018',
'ClientEmail'=>'wdpjaipur1#gmail.com',
'ClientExternalIdentifier'=>'Wdp tech',
'ClientIP'=>$clientIP,
'ClientPhone'=>'1234567890',
'ClientDOB'=>'1989-11-05'
);
$ch = curl_init();
$post_variables = '';
foreach ($request_arr as $key => $value) {
$post_variables .= "{$key}={$value}&";
}
$post_variables = rtrim($post_variables, '&');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
$responceData = json_decode($response, TRUE);

How do I check my code using the Sphere Engine API?

From http://sphere-engine.com/services/problems
Sending requests
Follow the example of sending request to the service:
curl -H "Content-Type: application/json" -X POST
-d '{
"code":"EXDOC"
"compilerId":"11",
"source":"#include<stdio.h> int main(){ // empty solution }"
}'
"https://problems.sphere-engine.com/api/v3/submissions?access_token=xxxxxx"
I created a problem called HELLO_WORLD but when I send a post request for the problem I get
{"message":"Forbidden","data":[]}
I have checked that my access token is correct as it works for other calls.
$token = '***********';
$get_submission_id = curl_init();
curl_setopt_array($get_submission_id, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://api.compilers.sphere-engine.com/api/v3/submissions?access_token=*******',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
language => $lang,
input => $input,
sourceCode => $code
)
));
$get_ideone_results = curl_init();
$submissions_id = $results->id;
$url = 'http://api.compilers.sphere-engine.com/api/v3/submissions/'.$submissions_id.'/?access_token=***********&withSource=1&withInput=1&withOutput=1&withStderr=1&withCmpinfo=1';
usleep(2000000);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
kindly check the code and share your thoughts
i think that's because the problem code (EXDOC) does not exist. try creating a new problem and use its problem code

Unsupported content with type: multipart/form-data in google plus api oauth2

I am trying to sign in with google plus Api in my web app.
I searched on it from https://developers.google.com/oauthplayground/https://developers.google.com/oauthplayground/
and I applied them.
When I request to https://www.googleapis.com/oauth2/v3/token , it returns
{ "error": "internal_failure", "error_description": "Unsupported content with type: multipart/form-data; boundary=----------------------------5dd1639f2986" }
I am putting my code snippets which are doing the request (as client id and secret as star)
I dont understand it, maybe you can help.
if(isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://www.googleapis.com/oauth2/v3/token';
$params = array(
"code" => $code,
"client_id" => "************************",
"client_secret" => "************************",
"redirect_uri" => "http://localhost/googleplus/oauth2callback.php",
"grant_type" => "authorization_code"
);
$json=CallAPI('POST',$url,$params);
and my CALLAPI function
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data){
$url = sprintf("%s?%s", $url, http_build_query($data));
echo $url;
}
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
You request is correct but missing something
the content type is not multipart/form-data should be application/x-www-form-urlencoded instead
I had very similar problem using OAuth2.0 to access Gmail from iOS app. Indeed Ahmad's suggestion is correct I'll expand it:
When you POST using HTTP there is a header field 'Content-Type:'. Looks like in your case it had default value of 'multipart/form-data' which is for binary data. For Google API OAuth2.0 make sure you set it to 'application/x-www-form-urlencoded'.
You can see this value in the example given here:
https://developers.google.com/identity/protocols/OAuth2InstalledApp#handlingtheresponse
You can read more about those two values for 'Content-Type' here:
application/x-www-form-urlencoded or multipart/form-data?
In my iOS framework I had to make a call to customize this HTTP header. Unfortunately I'm not familiar with PHP so hope that helps.
I had a very similar problem. I was finally able to resolve it by adding some elements to the HTTP header ("Content-Type" and "Content-Length") and manually constructing the url (so I can use 0 for "Content-Length").
$id_token = $_POST['id_token'];
$url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='.$id_token;
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array( "Content-Length: 0", "Content-Type: application/x-www-form-urlencoded"),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
];
$curlObj = curl_init();
curl_setopt_array($curlObj, $options);
$returnData = curl_exec($curlObj);
if (curl_errno($curlObj)) {
//error message
$returnData = curl_error($curlObj);
}
echo $returnData;
if(isset($_REQUEST['code'])){
$ch =curl_init();
$auth_url = "https://www.googleapis.com/oauth2/v4/token";
$post = array(
'code' =>$_REQUEST['code'],
'client_id' => '**************',
'client_secret' => '************',
'redirect_uri' => 'http://localhost/prakash/redirect.php',
'grant_type' => 'authorization_code'
);
$postText = http_build_query($post);
$options = array(
CURLOPT_URL =>$auth_url,
CURLOPT_POST => true,
CURLOPT_SSL_VERIFYPEER =>false,
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_POSTFIELDS => $postText
);
//print_r($ch);
curl_setopt_array($ch, $options);
$returnData = curl_exec($ch);
print_r($returnData);
curl_close($ch);
exit;
}else{
print_r($_REQUEST);
exit;
}