MailGun API doesn't output JSON - php

I am trying to test sending emails using the Mailgun api. I am using PHP to interface with the api. Following is the code that I have tried (from here).
# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$domain = "samples.mailgun.org";
# Make the call to the client.
$result = $mgClient->sendMessage("$domain",
array('from' => 'Excited User <me#samples.mailgun.org>',
'to' => 'Baz <baz#example.com>',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!'));
var_dump($result);
Now when I try the API, I get a response similar to the following:
stdClass Object ( [http_response_body] => stdClass Object ( [message] => Queued.
Thank you. [id] => <12345678901234.1234.12345#samples.mailgun.org> )
[http_response_code] => 200 )
How do I assign this output to an array or convert this to simple JSON using PHP? Is there some in-built PHP function which would format the above output to simple JSON or do I need to do something else. I have beginner level PHP skills.
Any help would be very appreciated. Thanks!
P.S.: The mailgun api key used above is from the MailGun API documentation.
UPDATE: Thanks guys. I got it working.
$darr=json_encode($result);
$data= json_decode($darr,true);
# Prints out the individual elements of the array
echo $data["http_response_body"]["message"]."<br>";
echo $data["http_response_body"]["id"]."<br>";
echo $data["http_response_code"];

You could try the php built-in function json_encode().
http://us3.php.net/json_encode

Related

Smsgateway.me with visual foxpro, is PHP

I'm trying to use the SMS gateway from smsgateway.me in a software that works in VFP.
Anyone can help me to implement the PHP code in VFP?
The code is in: https://smsgateway.me/sms-api-documentation/messag...
PHP Code:
require 'vendor/autoload.php';
use SMSGatewayMe\Client\ApiClient;
use SMSGatewayMe\Client\Configuration;
use SMSGatewayMe\Client\Api\MessageApi;
use SMSGatewayMe\Client\Model\SendMessageRequest;
// Configure client
$config = Configuration::getDefaultConfiguration();
$config->setApiKey('Authorization', 'your-token-here');
$apiClient = new ApiClient($config);
$messageClient = new MessageApi($apiClient);
// Sending a SMS Message
$sendMessageRequest1 = new SendMessageRequest([
'phoneNumber' => '07791064781',
'message' => 'test1',
'deviceId' => 1
]);
$sendMessageRequest2 = new SendMessageRequest([
'phoneNumber' => '07791064781',
'message' => 'test2',
'deviceId' => 2
]);
$sendMessages = $messageClient->sendMessages([
$sendMessageRequest1,
$sendMessageRequest2
]);
print_r($sendMessages);
Look into using Chilkat to send SMS through VFP, instead of the native foxpro ways.
Chilkat made it very easy for me, sending and receiving text messages (with or without media files) from within foxpro code. Where you can automate things, putting them into DO/ENDDO loops etc. I use Twilio services but the same concept should work with smsgateway.
Good luck,
Sime
Thanks for help.
I find another way, and use a Android APK to create a SMS gateway direct in the phone.
I gona post another asking for help

Api call in php with credentials

I'm using the cURL setup on this page to make my API calls in php.
The function call goes as follows: callAPI($method, $url, $data). I've made it work with a ('GET', 'URL', false) API call so the code does work.
Now I want to make a call to a different api. The data inputs I need to give are these:
Array
(
[Credentials] => Credentials Object
(
[Account] => my_account_name
[Password] => my_password
)
)
So this will need to be my $data object I believe?
I'm new to php so I don't know how I should setup my code to make this work with these data inputs.
Can somebody please help me how I should make these inputs in my API call?
Thanks in advance!

Mailgun configure for PHP

I'm new on mailgun, there i found php code but unable to understand all for this asking help on here. please help me anyone.
require 'vendor/autoload.php';
use Mailgun\Mailgun;
$mgClient = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$domain = "samples.mailgun.org";
$result = $mgClient->sendMessage("$domain",
array('from' => 'Excited User <excited#samples.mailgun.org>',
'to' => 'Mailgun Devs <devs#mailgun.net>',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomeness!'));
But where i find autoload.php. please help me anyone. also their girhub project showing error.
Thanks in advance.
If you go here, you will find all instructions for sending email through MailGun.
Click on Library download to download all the MailGun files with the Composer.
Then you just have to place the autoload.php correctly in your Mailgun folder to match with your code, and it'll work like a charm.
Note that the Mailgun's response is in JSON, use
echo "<pre>";
print_r($results);
echo "</pre>";
to print your results in an array way. It's easier to read and to analyse.

SAML - Getting response from the identity provider

I am trying to implement SAML authentication system using onelogin.com, I am using this php library https://github.com/simplesamlphp/saml2 with below code
// Set up an AuthnRequest
$request = new SAML2_AuthnRequest();
// $request->setId(SAML2_Utils::generateId());
$request->setIssuer('http://localhost:8888/yii2/dw/advanced/frontend/web/index.php?r=site/auth');
$request->setDestination('https://app.onelogin.com/trust/saml2/http-post/sso/418578');
// Send it off using the HTTP-Redirect binding
$binding = new SAML2_HTTPRedirect();
$binding->send($request);
Above code is successfully authenticating me and posting back me to my success page, but I don't know how to get user information from posted data.
I am trying below code to get the user data
$response = new \SAML2_Response();
print_r($response);
It is giving below data
SAML2_Response Object
(
[assertions:SAML2_Response:private] => Array
(
)
[inResponseTo:SAML2_StatusResponse:private] =>
[status:SAML2_StatusResponse:private] => Array
(
[Code] => urn:oasis:names:tc:SAML:2.0:status:Success
[SubCode] =>
[Message] =>
)
[extensions:protected] =>
[tagName:SAML2_Message:private] => Response
[id:SAML2_Message:private] => _afe4d7fd7add270de7d334231e2eec68d1492363130
[issueInstant:SAML2_Message:private] => 14340322405
[destination:SAML2_Message:private] =>
[consent:SAML2_Message:private] => urn:oasis:names:tc:SAML:2.0:consent:unspecified
[issuer:SAML2_Message:private] =>
[relayState:SAML2_Message:private] =>
[document:protected] =>
[signatureKey:SAML2_Message:private] =>
[messageContainedSignatureUponConstruction:protected] =>
[certificates:SAML2_Message:private] => Array
(
)
[validators:SAML2_Message:private] => Array
(
)
)
From above output I am not able to get user information, Please help.
Your code:
$response = new \SAML2_Response();
actually creates a new SAML 2.0 Response object instead of parsing the one that is posted back from the IDP. This call should be used by an IDP that wants to create a SAML response. You should be looking to execute something like the following code on the Assertion Consumer (ACS) URL:
$b = SAML2_Binding::getCurrentBinding();
$response = $b->receive();
but I would very strongly advise you to use simpleSAMLphp itself (or another 3rd party SAML implementation) for integration SAML in to your PHP application since it deals with all complex and security sensitive SAML processing.
The SAML 2.0 library is only meant for developers who want to use SAML for other purposes than Web SSO or that want to rebuild a SAML implementation because simpleSAMLphp does not suit them. Assuming you have no requirements that simpleSAMLphp can't meet, you're far better off (and secure) using that. Moreover, if you'd have an extension requirement, I would create a pull request for simpleSAMLphp instead of redoing that project from scratch.
#HansZ is right. Be careful when using directly the SAML core of simpleSAMLphp instead the whole framework.
I also wanted to suggest an alternative, since you want to connect your application with onelogin.com, why not use the OneLogin's PHP SAML Toolkit? https://github.com/onelogin/php-saml

add followers to asana task using api

I just don't know why this fails. I have the following which attempts to add tasks to Asana:
$arrayOfIds = array("0123456789", "9876543210");
$followers = implode('", "', $arrayOfIds);
$newtask = $asana->createTask(array(
"workspace" => $workspaceId,
"name" => $name,
"team" => $teamId,
"assignee" => $assignee,
"due_on" => $dueOn,
"completed" => $completed,
"completed_at" => $completedAt,
//"followers" => array("0123456789")
"followers" => array($followers)
));
It works just fine when I put the user ID in manually, like above commented code, however fails when I try to use the array. I get the following error:
Error while trying to connect to Asana, response code: 400
Any help is greatly appreciated.
Without knowing exactly how the $asana lib is implemented (link?) my guess would be that the array is not encoded correctly. If sent via url-encoded post data (as opposed to JSON) the array should be comma-delimited. PHP may do something else like followers[]=1&followers[]=2 when it should be followers=1,2.
Try doing something like "followers" => implode(",", $arrayOfIds), or check the code of the library to see how it encodes arrays. Additionally, including the actual HTTP request/response would help figure out what's going in. There are many ways to get this - your library may include a verbose/debugging mode, but if all else fails you can always use something like Charles Proxy, Wireshark, Ettercap, etc.

Categories