My Code is as follows...
The email functions
public function zoho_email($array){
$data = json_decode($array,true);
$url = '/invoices/'.$data['invoice']['invoice_id'].'/email';
$recivers[] = array($data['invoice']['contact_persons_details'][0]['email']);
$data = array(
'to_mail_ids' => $recivers,
'subject' => 'Invoice from MSL (Invoice#: '.$data['invoice']['invoice_number'].')',
'body' => 'Dear Customer,<br><br><br><br>Thanks for your business,
'send_from_org_email_id' => true
);
$result = $this->zoho_create($url, $data);
}
The curl Function for create invoices, contacts, and send email
public function zoho_create($url,$array){
$json = json_encode($array);
$data = array('authtoken' => ZOHOAUTHTOKEN,'JSONString' => $json,'organization_id' => ZOHOORGNISATIONID);
$curl = curl_init($this->apiUrl.$url);
if($url=='contacts/'){
curl_setopt_array($curl, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
));
}
else{
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded") );
}
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
I want to send invoice by email through API to customer but This error occur in my code.
{"code":5,"message":"Invalid URL Passed"}
Please help me out there....
Thanks in advance...
Your code is working correctly. Try to print the url ($url) and confirm once if it is in the required format (/invoices/invoice_id/email). For example if your invoice_id is 1234, then the $url should be '/invoices/1234/email'. Also make sure that $this->apiUrl is https://books.zoho.com/api/v3
If there occurs a problem still u can use the help documents mentioned below:
https://www.zoho.com/books/api/v3/.
have you tried using full url : "https://books.zoho.com/api/v3/invoices/:invoiceno/email"
Related
Good day family !
Please help me using this API.
This API is user to get a token see the normal answer :
{'status': True, 'message': 'Success', 'code': 50, 'data': {'token': '773b718a53341b57zd511dazd4588fdf2', 'amount': 45.0}}
When i use Python it's working fine ! here is the python working code :
import requests
import json
headers = {"Authorization": "Token 12345678900987654321234567890", "Content-Type":"application/json"}
data = {"vendor": 1265,
"amount": 45,
"note" : "Commande #1324"
}
r = requests.post("https://sandbox.paymee.tn/api/v1/payments/create", data=json.dumps(data), headers=headers)
response = r.json()
# Now you can use
print(response)
But when i use PHP i have as output : NULL
Here is the code
<?php
$headers = array("Authorization: Token 12345678900987654321234567890");
$curl_post_data = array(
'vendor' => 1265,
'amount' => 45,
'note' => "Commande #1324"
);
$curl_handle=curl_init('https://sandbox.paymee.tn/api/v1/payments/create');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_se
topt($curl_handle, CURLOPT_POSTFIELDS, $curl_post_data);
$query = json_decode(curl_exec($curl_handle),true);
var_dump($query);
curl_close($curl_handle);
?>
Do you have any solution please ? i spent one week stuking there ! and my exam is close... :(
Thanks in advance
$result = curl_exec($curl_handle);
$query = json_decode($result,true);
<?php
$headers = array(
"Authorization: Token 12345678900987654321234567890",
"Content-Type: application/json"
);
$data = array(
"vendor" => 1265,
"amount" => 45,
"note" => "Commande #1324"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sandbox.paymee.tn/api/v1/payments/create");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
print_r($response);
?>
This gives me the response that I'm using a invalid token which seems about right since you gave a sample token.
I'm trying to submit a new paste to Paste.ee API using Curl, but the response is always Whoops, looks like something went wrong.
Here's the guideline of Paste.ee API: https://paste.ee/wiki/API:Basics
Here's how my simple code looks like:
<?php
$key = '56b3f127d65445c0cf6ff05f62ffba53';
$format = 'JSON';
$description = 'just for testing';
$paste = 'Just a simple test';
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://paste.ee/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("key" => $key, "format" => $format, "description" => $description, "paste" => $paste));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
} else {
error_log("You need cURL to use this api!");
}
?>
I've double-checked, my key is right.
I'm new to Curl, so any advice would be very appreciated.
Thanks.
It's because "format" is case sensitive in which it must small letters
You your format is capitalised which is it should be small letters
"format": "json"
look
$key = '56b3f127d65445c0cf6ff05f62ffba53';
$format = 'JSON';
$description = 'just for testing';
$paste = 'Just a simple test';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://paste.ee/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'key' => $key,
'format' => $format,
'description' => 'just for testing',
'paste' => $paste,
'format' => 'json'
));
CAPITALISED WHICH RETURNS ERROR
small letters which returns the correct json data
All I want is basically to login, display info from a simple filter like all open bugs in a milestone. Once I get that far ill be able to work off the documentation
I'm hoping to use simple PHP, that's because I'm new to PHP so the more complicated the coding is the harder it will be for me to figure it out.
This script is the closest I've come to being successful:
<?php
define('JIRA_URL', 'https://mysite.atlassian.net');
define('USERNAME', 'me#gmail.com');
define('PASSWORD', '11111');
function post_to($resource, $data) {
$jdata = json_encode($data);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => JIRA_URL . '/rest/api/2/' . $resource,
CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
function create_issue($issue) {
return post_to('issue', $issue);
}
$new_issue = array(
'fields' => array(
'project' => array('key' => 'FOO'),
'summary' => 'Test via REST',
'description' => 'Description of issue goes here.',
'issuetype' => array('name' => 'Story')
)
);
$result = create_issue($new_issue);
if (property_exists($result, 'errors')) {
echo "Error(s) creating issue:\n";
var_dump($result);
} else {
echo "New issue created at " . JIRA_URL ."/browse/{$result->key}\n";
var_dump($result);
}
?>
It returns New issue created at http://mysite.atlassian.net/browse/
But when I put the var_dump ($result) in there that just returns null so I have no idea if the jira URL is wrong or my password or if its outdated code for the new rest API.
If I could just make 1 simple query to the API and see anything returned on my PHP page I would be a happy camper. The example above creates a new issue, that's just the example I was using but I'm fine with just returning any sort of data so my main issue is getting a valid connection and returning some info so I know its working and can't start trying some different things then.
I have admin access but I wasn't clear if I needed to enable something on the admin side.
I figured out what i needed to do, there are lots of examples on the net but i couldnt find one simple like this so hopefully it helps someone else
$username = 'xxx';
$password = 'xxx';
$url = 'https://xxx.atlassian.net/rest/api/2/Issue/Bug-5555';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$issue_list = (curl_exec($curl));
echo $issue_list;
Following to this page...
https://developer.atlassian.com/jiradev/api-reference/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-basic-authentication
We need to use header authentication these days which looks - modifying the example in this tread - like:
$username = 'user';
$password = 'pass';
$url = 'https://www.jiradomain.com/rest/api/2/issue/PROJECT-321/worklog';
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$username:$password")) );**
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($curl, CURLOPT_STDERR, $verbose);
$issue_list = (curl_exec($curl));
echo $issue_list;
$result = curl_exec($curl);
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($curlHandle),
htmlspecialchars(curl_error($curlHandle)));
}
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
I need to send some data from my form in html to webservice. (For do this, i need to do the operation of POST)
I have seen research that I can transmit information using php cURL. But in all examples, i don't view to send data to webservice, only to file php that print $_POST variable.
I have this webservice: http://192.168.1.1/fastfood/event/attendee (example)
And i try to send data in an array.
For example i try to send: attendee = array( 'name' => $_POST['name'] , 'lastname' => $_POST['lastname'] , 'address' => $_POST['address'] );
Then, the web service takes out the array data. ¿How to do this?
UPDATE 1:
This is my code that i'm doing now... But don't work :(
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$attendee = array(
'name' => "$name",
'lastname' => "$lastname",
'address' => "$address"
);
$url_target = 'http://192.168.1.1/fastfood/event/attendee';
//$header = array('Content type: multipart/form-data');
$user = 'root';
$pass = '123';
$userpasswd = "$user:$pass";
$ch = curl_init($url_target);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $userpasswd);
//curl_setopt($ch, CURLOPT_URL, $url_target);
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
$getInfo = curl_getinfo($ch);
curl_close($ch);
The variable $result return me a FALSE, and the variable $getInfo return me a http_code = 500, Content-Type = Null.
Reading the documentation of cURL when i send a data like a array, the content type should be a "multipart/form-data" but, also, don't work for me.
// Here is the data we will be sending to the service
$data = array(
'name' => $_POST['name'],
'lastname' => $_POST['last_name'],
'address' => $_POST['address']
);
$curl = curl_init('http://192.168.1.1/fastfood/event/attendee');
curl_setopt($curl, CURLOPT_POST, 1); //Choosing the POST method
curl_setopt($curl, CURLOPT_URL, 'http://localhost/helloservice.php'); // Set the url path we want to call
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Make it so the data coming back is put into a string
curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data); // Insert the data
// Send the request
$result = curl_exec($curl);
// Free up the resources $curl is using
curl_close($curl);
echo $result;
Written by Chad Lung at GiantFlyingSaucer
How do you pass $_POST values to a page using cURL?
Should work fine.
$data = array('name' => 'Ross', 'php_master' => true);
// You can POST a file by prefixing with an # (for <input type="file"> fields)
$data['file'] = '#/home/user/world.jpg';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
curl_close($handle)
We have two options here, CURLOPT_POST which turns HTTP POST on, and CURLOPT_POSTFIELDS which contains an array of our post data to submit. This can be used to submit data to POST <form>s.
It is important to note that curl_setopt($handle, CURLOPT_POSTFIELDS, $data); takes the $data in two formats, and that this determines how the post data will be encoded.
$data as an array(): The data will be sent as multipart/form-data which is not always accepted by the server.
$data = array('name' => 'Ross', 'php_master' => true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$data as url encoded string: The data will be sent as application/x-www-form-urlencoded, which is the default encoding for submitted html form data.
$data = array('name' => 'Ross', 'php_master' => true);
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
I hope this will help others save their time.
See:
curl_init
curl_setopt
Ross has the right idea for POSTing the usual parameter/value format to a url.
I recently ran into a situation where I needed to POST some XML as Content-Type "text/xml" without any parameter pairs so here's how you do that:
$xml = '<?xml version="1.0"?><stuff><child>foo</child><child>bar</child></stuff>';
$httpRequest = curl_init();
curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($httpRequest, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($httpRequest, CURLOPT_POST, 1);
curl_setopt($httpRequest, CURLOPT_HEADER, 1);
curl_setopt($httpRequest, CURLOPT_URL, $url);
curl_setopt($httpRequest, CURLOPT_POSTFIELDS, $xml);
$returnHeader = curl_exec($httpRequest);
curl_close($httpRequest);
In my case, I needed to parse some values out of the HTTP response header so you may not necessarily need to set CURLOPT_RETURNTRANSFER or CURLOPT_HEADER.
Another simple PHP example of using cURL:
<?php
$ch = curl_init(); // Initiate cURL
$url = "http://www.somesite.com/curl_example.php"; // Where you want to post data
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n"); // Define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
$output = curl_exec ($ch); // Execute
curl_close ($ch); // Close cURL handle
var_dump($output); // Show output
?>
Instead of using curl_setopt you can use curl_setopt_array.
http://php.net/manual/en/function.curl-setopt-array.php
$query_string = "";
if ($_POST) {
$kv = array();
foreach ($_POST as $key => $value) {
$kv[] = stripslashes($key) . "=" . stripslashes($value);
}
$query_string = join("&", $kv);
}
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$url = 'https://www.abcd.com/servlet/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$url='Your url'; // Specify your url
$data= array('parameterkey1'=>value,'parameterkey2'=>value); // Add parameters in key value
$ch = curl_init(); // Initialize cURL
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
<?php
function executeCurl($arrOptions) {
$mixCH = curl_init();
foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
}
$mixResponse = curl_exec($mixCH);
curl_close($mixCH);
return $mixResponse;
}
// If any HTTP authentication is needed.
$username = 'http-auth-username';
$password = 'http-auth-password';
$requestType = 'POST'; // This can be PUT or POST
// This is a sample array. You can use $arrPostData = $_POST
$arrPostData = array(
'key1' => 'value-1-for-k1y-1',
'key2' => 'value-2-for-key-2',
'key3' => array(
'key31' => 'value-for-key-3-1',
'key32' => array(
'key321' => 'value-for-key321'
)
),
'key4' => array(
'key' => 'value'
)
);
// You can set your post data
$postData = http_build_query($arrPostData); // Raw PHP array
$postData = json_encode($arrPostData); // Only USE this when request JSON data.
$mixResponse = executeCurl(array(
CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_VERBOSE => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CUSTOMREQUEST => $requestType,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"X-HTTP-Method-Override: " . $requestType,
'Content-Type: application/json', // Only USE this when requesting JSON data
),
// If HTTP authentication is required, use the below lines.
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username. ':' . $password
));
// $mixResponse contains your server response.