Sending Email with Mailgun via HTTP Request using Curl - api

I am having some issue running the api to send email with attachment via Mail Gun.I tried to run the api via postman and it runs perfectly fine so i know there is no problem with the api but i am unable to send a request via code.
I have added a code sample below:
<?php
$curl_post_data=array(
'from' => 'Excited User <noreply#test.com>',
'to' => 'test#gmail.com',
'subject' => 'Hello',
'text' => 'test'
//,'attachment[1]' => '#https://www.smsglobal.com/docs/HTTP-2WAY.pdf'
,array( 'attachment' => 'https://www.smsglobal.com/docs/HTTP-2WAY.pdf')
);
$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-testtesttesttes");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>

I was able to solve the issue.
My Findings:
1-) You cannot give a live url address as attachment http://test.com/images/logo.png will not work.
2-) only address of files hosted on the server can be given
/var/images/logo.png
<?php
$filePath='#0Wealth_AC_AMF.pdf';
$curl_post_data=array(
'from' => 'Excited User <noreply#test.com>',
'to' => 'test#gmail.com',
'subject' => 'Hello',
'text' => 'test',
'attachment[1]' => $filePath
);
$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-test");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>

Related

Vanilla API v2 Post Request (Curl) not working?

I'm trying to add a new user to my Vanilla forum and unfortunately I don't understand what's wrong with my code. GET request to read user data works fine butPOST requests result in a "bool(false)".
As I understand it, that's already a response from the API, which obviously can't do anything with my request, right?
But what's wrong with that? Do I have to somehow send the JSON data as a 'body'? Anyone have an idea? Thank you!!!
This is what my code currently looks like:
<?php
// post with absolute url = Invalid CSRF token error
// $url = "https://example.com/app/vanilla/api/v2/users";
$url = "/app/vanilla/api/v2/users";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
$headers = array(
"Accept: application/json",
"Authorization: Bearer {va.jOh189hry7463klur_kcw8UomJU36782.HGqvdQ.RQon456}"
);
$data = array(
'bypassSpam' => 'false',
'email' => 'info#mysite.de',
'emailConfirmed' => 'true',
'name' => 'Theo Tester',
'password' => '1234567890',
'photo' => '',
'roleID' => [33]
);
$payload = json_encode(array($data));
// Attach encoded JSON string to the POST fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// Return response instead of outputting
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
This is what the vanilla API documentation looks like (please see picture below or link):
(https://success.vanillaforums.com/kb/articles/202-api-v2-reference-endpoints#/Users/post_users)
And this just for completeness is the code for GET requests as it works for me:
<?php
$url = "https://example.com/app/vanilla/api/v2/roles";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Bearer {va.jOh189hry7463klur_kcw8UomJU36782.HGqvdQ.RQon456}",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>

Zoho crm API sometimes returns error

I'm trying to searchRecord using the Zoho CRM API and I sometimes get the following error:
Array ( [response] => Array ( [error] => Array ( [message] => Unable
to process your request. Please verify whether you have entered proper
method name,parameter and parameter values. [code] => 4600 ) [uri] =>
/crm/private/json/Contacts/searchRecords ) )
My problem is that sometimes everything works fine and sometimes I get this error
define("TARGETURL", "https://crm.zoho.com/crm/private/json/Contacts/searchRecords");
$parameter = array(
'scope' => 'crmapi',
'authtoken' => AUTHTOKEN,
'selectColumns' => 'All',
'criteria' => '(Account Name:'.$accountName.')',
'fromIndex' => $fromIndex,
'toIndex' => $toIndex
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TARGETURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
$result = curl_exec($ch);
curl_close($ch);
Try search field with double quotation mark, following is script to search email from lead module.
$xmlData = "(Email:$email)";
$ch = curl_init('https://crm.zoho.com/crm/private/xml/Leads/searchRecords');
curl_setopt($ch, CURLOPT_VERBOSE, 1); //standard i/o streams
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Turn off the server and peer verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set to return data to string ($response)
curl_setopt($ch, CURLOPT_POST, 1); //Regular post
$authtoken = "********************************";
$query = "authtoken=" . $authtoken . "&scope=crmapi&criteria=" . $xmlData;
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // Set the request as a POST FIELD for curl.
$response = curl_exec($ch);
curl_close($ch);

Input not file while creating App via Phonegap build API

I am trying to create a phonegap app by using their createapp build API as described in
http://docs.build.phonegap.com/en_US/developer_api_write.md.html
<?php
$data = array(
"file" => "#http://mywebsite.com/tmp/www.tar.gz",
//"file" => "#actual path to/www.tar.gz",
"data" => '{"title":"MyTestApp","create_method":"file"}'
);
$myusername = 'my build.phonegap user id';
$password = 'my build.phonegap password';
$handle = curl_init();
curl_setopt($handle, CURLOPT_USERPWD, $myusername.':'.$password);
curl_setopt($handle, CURLOPT_URL, "https://build.phonegap.com/api/v1/apps");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
print_r($response);
?>
The www.tar.gz file is a valid file and it can be even download from the URL.
The code shows the below error
{"error":"Unable to create app: input is not a file"}
I raised this with phonegap support website and It looks my question doesn't get anyone's attention there. :-(
https://c.getsatisfaction.com/nitobi/topics/unable-to-create-app-input-is-not-a-file-df668pmjm93np
Thanks #mradionov at https://github.com/mradionov/phonegap-build-api/issues/3
The problem is just because of the cURL changed in php and nothing to do with the phonegap api releases.
It looks the file upload method via curl got changed in php 5.6 version.
The below solved the issue
<?php
$data = array(
"file" => new CurlFile("path to the file/abcd.tar.gz"),
"data" => '{"title":"MyTestApp","create_method":"file"}'
);
$myusername = 'phonegap user id';
$password = 'phonegap password';
$handle = curl_init();
curl_setopt($handle, CURLOPT_USERPWD, $myusername.':'.$password);
curl_setopt($handle, CURLOPT_URL, "https://build.phonegap.com/api/v1/apps");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
print_r($response);
?>
In case if we use the OAuth then use the below
<?php
$data = array(
"file" => new CurlFile("path to file/abcd.tar.gz"),
"data" => '{"title":"MyTestApp","create_method":"file"}'
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, "https://build.phonegap.com/api/v1/apps?access_token=accesstoken");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
print_r($response);
?>
#Malaiselvan,
Phonegap 2.x is obsolete. It is no longer supported. None of those examples work any longer.
As of 23 Sep 2015, the blog post is entitled PhoneGap 5.2.0 Now Available on Build.
The means we are on Phonegap version 5.x.
If you want build your own remote IDE, then you want to use this documentation
http://docs.build.phonegap.com/en_US/developer_api_api.md.html#PhoneGap%20Build%20Developer%20API
Best of Luck

Updating Event Details using Eventrite API v3

I'm trying to edit Eventbrite event using version 3 of the API but I always returns false.
$request_url = 'https://www.eventbriteapi.com/v3/events/xxxxxx';
$options = array(
'http' => array(
'method' => 'POST',
'header'=> "Authorization: Bearer xxxxxxxxxx"
),
'name' => array(
'text'=>'Hi',
'html'=>'<p>Hi</p>'
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
var_dump($json_data);
This always returns me :
boolean false
What am I doing wrong?
You don't need to create an options array with curl as you are setting those options in curl_setopt. Just make a data array and submit it.
<?php
$request_url = 'https://www.eventbriteapi.com/v3/events/xxxxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'name' => array(
'text'=>'Hi',
'html'=>'<p>Hi</p>'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
var_dump($json_data);
Though according to the docs there is a minimum amount of data to create an event.

How to login using mediawiki API?

$ch=curl_init();
$postfield = "action=login&lgname=d&lgpassword=Password&format=json";
$url = "http://wiki.signa.com/api.php"; //url to wiki's api
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = json_decode(curl_exec($ch));
curl_close($ch);
print_r($output);
$token = $output->login->token;
$session = $output->login->sessionid;
$ch=curl_init();
$postfield = "action=login&lgname=d&lgpassword=Password&lgtoken={$token}";
$url = "http://wiki.signa.com/api.php"; //url to wiki's api
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
die;
With the first request I get this object:
stdClass Object
(
[login] => stdClass Object
(
[result] => NeedToken
[token] => ad61fadb829e5cd44b0062463b7cc2d2
[cookieprefix] => wikisign_mediawiki
[sessionid] => ebb892881eed27554161234916d00480
)
)
I'm using the token to do a second request, but I get result = NeedToken. It should be success since I'm sending the token now.
I noticed the documentation says:
Send a login request with POST, with confirmation token in body and the session cookie (e.g. enwiki_session) in header as returned from previous request.
I'm not totally clear on that. I'm assuming I'm having issues because I'm not sending the session cookie in the header. Do I need to set a cookie prior to the second request?
Yes, you need to process all Set-Cookie Headers to login succesfully.
Documentation is very specific on this:
This request will also return a session cookie in the HTTP header
(Set-Cookie: enwikiSession=17ab96bd8ffbe8ca58a78657a918558e; path=/;
domain=.wikipedia.org; HttpOnly) that you have to return for the
second request if your framework does not do this automatically
A successful action=login request will set cookies needed to be
considered logged in. Many frameworks will handle these cookies
automatically (such as the cookiejar in cURL). If so, by all means
take advantage of this. If not, the most reliable method is to parse
them from the HTTP response's Set-Cookie headers.
It's an old subject, but I'll answer for those who are interested :)
In fact you need to keep your cookies in a file and use them again in your second attempt to login.
Something like that :
$path_cookie = 'connexion_temporaire.txt';
if (!file_exists($path_cookie)) touch($path_cookie);
$postfields = array(
'action' => 'login',
'format'=> 'json',
'lgname' => $login,
'lgpassword' => $pass
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, $path_cookie); // you put your cookie in the file
$connexion=curl_exec($curl);
$json_connexion = json_decode($connexion, true);
$tokenConnexion=$json_connexion['login']['token']; // you take the token and keep it in a var for your second login
// /!\ don't close the curl conection or initialize a new one or your session id will change !
$postfields = array(
'action' => 'login',
'format'=> 'json',
'lgtoken' => $tokenConnexion,
'lgname' => $login,
'lgpassword' => $pass
);
curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEFILE, $path_cookie); //get the previous cookie
$connexionToken=curl_exec($curl);
var_dump($connexionToken);
When running this you should see a success this time :)