AWS Lambda - Invoke by straight PHP curl - php

Alright,
So I'm aware of the AWS PHP SDK. I do not want to use it to make a curl call to invoke a lambda function.
I've found things about using API Gateway. I also do not want to use API Gateway. I just want to be like:
$endPoint = "https://urltoinvokethefunction"
$ch = curl_init();
$chOptions = [
CURLOPT_URL => $endPoint,
//credentials, headers, etc etc
];
curl_setopt_array($ch, $chOptions);
$result = curl_exec($ch);
Is this at all possible? I want it to be as simple as possible, just a straight curl call to invoke the function. As far as I understand it's what is happening in the SDK anyway, so it must be possible without using the SDK.
I know the most sensible way would be to use the SDK, and I have used it successfully before. I'm up against an unfortunate scenario of trying to add in custom business logic to an outdated, spider web of a CMS, and where the SDK is already included in a plugin I can't simply reuse the already namespaced SDK in a custom function.
tldr; writing a publish_post hook in Wordpress, can't use the AWS SDK because a plugin is already using it, just looking to make a straight curl call to invoke the function, not going to use API Gateway, how to?
****** UPDATE ******
Solved my problem, hanging head low... the reason I couldn't use the already loaded SDK was a versioning issue... d'oh. Stuck using a super old version for the time being, but it works.
The question still remains though, because I'm a stickler for simplicity, and I'd rather just know how to do it myself rather than import a super huge library (sledgehammer to hammer a nail kind of thing). Curious to hear a solution!

$data = array(‘key’=>'key1');
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"ENTER_YOUR_PUBLIC_URL");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Content-Length: ‘ . strlen($data_string))
);
$result = curl_exec($ch);

Using AWS API Gateway is dead simple to link it to your Lambda function. You simply create a resource and stage, associate it to your Lambda function, and then deploy the API. From there, you'll receive a public URL to send requests to that will execute your Lambda function.
To my knowledge, this is the only way to execute a Lambda function over HTTP(S)

Related

PHP to Nodejs conversion for Pipedrive Deals

I am getting back into programming after being gone for 20 years. A lot has changed! lol...
I have a NodeJS server setup on Heroku for my mobile app. I am trying to add an event on my server that will add new user info to Pipedrive.com using their API.
They have only written their API examples in PHP. So I'm trying to translate PHP to Javascript, while also learning NodeJS, PHP, and understanding Pipedrive's API all at the same time.
They pointed me to Tonicdev which has been epically useful in getting my javascript syntax down. Since that uses live Pipedrive data when I add my token, I can do all my testing there too, before trying to upload and test on my actual Nodejs server. So that's handy!
But I'm still trying to get a grip on what's happening in their API code. This is my first time to implement an API by myself.
Here is the page I am trying to translate:
http://support.pipedrive.com/hc/en-us/articles/206679239-Creating-a-deal-using-the-REST-API-and-PHP-full-working-example-code-
I don't need the organization. Just the person and the deal.
In my create_person function, I found this php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $person);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
My questions are:
What is curl_init and curl_setopt?
Do I need them in my Nodejs script file for this?
If not, what javascript do I use in their place?
Thanks for your patience. Learning a ton here!!!
curl is a PHP library that handles making requests.
In this case curl_init() is initialising a new request and curl_setopt is setting certain options.
You'll want to replace curl with an equivalent library for NodeJS. The request module is pretty good for this, although there's plenty of other options too.

I need help authenticating to an API that uses OAUTH 2, using PHP

I am working in a web application that was coded using procedural PHP. No framework, no MVC, no OOP. It is what it is. At this point in time, recoding to use some sort of framework is not feasible. To my benefit, it is very well organized, and so it's easy to work within. Anyway - they want to add on a Point of Sales system, and have landed on Kounta (kounta.com). Kounta has an API that is RESTful and returns JSON or XML.
I am absolutely brand new to writing applications that integrate with API's, and there are a lot of terms being slung around that I am not quite familiar with.
From my understanding, I need to authenticate myself using oAuth 2.0, and from there, can make server calls to pull data from their server.
The first piece of that is what I need help with.
I have my client ID and client secret. I am just not sure what to do with them, and how to pass them to their server via script in order to receive a token, so that I can then make those server calls.
The Kounta API Documentation can be found here (http://www.kounta.com/documentation/).
Any help that anybody could provide would be greatly appreciated. At this point, I am not sure where to even get started.
Here is the code that I am currently using. This code returns an error asking me to identify myself to Kounta.
<?php
$url = "https://api.kounta.com/v1/companies/5678/orders.json?created_gte=2013‑06‑01";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
?>
I'm not too familiar with Kounta, but I recently did a project which required me to fetch data from Instagram based on a users ID.
To do this, I used CURL. Instagram has a pretty open API and even some examples. So it wasn't too hard for me to figure it out.
See below for my example:
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/<user>/media/recent/?access_token=<access_token>&count=4");
$result = json_decode($result);
foreach ($result->data as $post) {
echo '<div class="col-sm-3">
<a href="'.$post->images->standard_resolution->url.'" data-lightbox="instagram" data-title="'.$post->caption->text.' '.implode(' #',$post->tags).'">
<img src="'.$post->images->thumbnail->url.'" alt="" /></a>
</div>';
}
?>
What this does is simply connect to Instagrams API with a user ID and access token inside the URL and returns a JSON Array.
My only purpose for showing you this is because this might be exactly what you will need to do going forward with Kounta, in some form or fashion. I have not reviewed their API or anything so I can't say for sure. However, I'm more than certain CURL will be your best bet.
I would advise you to fully read through their API once more and see what options they have or maybe even see if they offer any examples to help you get started.
EDIT: You mentioned they they return JSON and XML. This is good to know because that more than likely means you can use CURL to get data from their databases.
Let me know if you have any questions.

how to delete member from chat room xmpp server via php

I am trying to delete a member of chat room from XMPP server via php. I am using curl request for that.
I am following this documentation:
https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-from-a-chat-room
$url = "http://188.***.***.***/plugins/restapi/v1/chatrooms/".$roomName."/members/".$userJID;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/xml", "Authorization : ******")); //I am using plugin.userservice.secret key here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
It should return me http response 201, but I am getting login form of server in response or 401 (unauthorized user).
I am trying to do this since last one week, but did not get any solution of this till, please help me.
Thanks in advance for your kind support.
Please note that this question if very specific: it relates to a specific XMPP server implementation (Openfire) and makes use of a proprietary, non-standard interface (its REST plugin). The fact that you're making use of an Android environment, PHP and/or cURL is irrelevant.
When you receive 401 responses, then there is a problem with authentication.
As Roman points out in a comment below, you're using the wrong documentation. Use this instead!
Two other observations that Roman made (out-of-band):
There's a surplus space character in "Authorization :" It need to be "Authorization:"
The property that you should use is plugin.restapi.secret, not plugin.userservice.secret.
Since this question is already well answered but I will like answer for Android specific context so other user coming to this question can find an alternate way.
There is a library for RestApiClinet for android here. You can integrate it directly as android module. Here is an app already using it. You can also have a look on this client library written in php.

How do I pass Variables from my site to another site without leaving my site?

I would like to be able to send variables to another website without actually going to the website using php.
I am building an ecommerce website where the shipping warehouse is being outsourced. After the person checks out with their products, I would like to send some variables over to the shipper's website using $_GET['vars']. This is a completely different URL. The problem is, I don't want the person actually going to the shipper's webpage. I just want to ping that info over there.
Is is possible to send Variables via URL to another site without leaving yours?
Yes, you can. the simplest way:
$contents = file_get_contents("http://example.com/some/page.php?var=abcd");
For more advanced features see Curl.
You should be storing all the relevant order info within your database then using cron to trigger a script that will process the unprocessed, this way systematic checks can be made on orders before any request to your outsource site. Dont rely on your users browser to hit a curtain point in the order process to trigger the API call or trust them not to triple click or inject values before submitting.
And I advise to use curl todo your actual request as its faster. Something as simple as:
<?php
function curl_do_api($url){
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
actually there is a more simpler way of solving this...using ajax
include the jquery header first
refer here
http://api.jquery.com/jQuery.ajax/
Both are right and you should make your choice based on security you're looking for.
cURL will be more secure and you should use it if you do not want to pass some argument in query string. At the same time when you pass data using file_get_cotents("URL?data=value"); you will have limit of about 2k for data being passed.
On the other side cURL will be secure if you use it with https it's much more secure. With cURL you will also be able to post files and emulate form post.

Integration of escrow web services in a PHP site

I want to integrate the services of escrow.com in my PHP site.
How would you get started with this goal, and what APIs provided would be the basic functionality? Do you have any PHP specific advice or gotchas? Would you recommend another service provider?
I'm working on an API project with this Company at the moment. I know looking at the documentation it all looks a little daunting, however, you can get away with making it as simple as a small cURL request.
I'd suggest starting with the "New escrow transaction" example provided, and build your request using the provided XML they offer, amended with your details.
Assign the XML to a variable, and pass it through a curl request similar to the below;
// Initialise your cUrl object
$ch = curl_init('https://xml.Escrow.com/Invoke/Partners/ProcessrequestXML.asp');
//set your cURL options
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "\$xmldata=".urlencode($xml));
//Start your cURL Transaction
ob_start();
//execute your cURL object with your parameters
$result = curl_exec($ch);
//set the returned info to a variable
$info = curl_getinfo($ch);
// close the transaction
curl_close ($ch);
//get the contents of the transaction
$data = ob_get_contents();
ob_end_clean();
//optional; Redirect to a specific place
header("Location:".$url);
The only advise I can offer is to read through the documentation carefully, and always check the values you are passing in.
Where possible, it is also a good idea to segregate the API functions into their own class, this will make maintenance and troubleshooting, as well as testing the functionality that much easier.
This is the first time I hear about escrow, but a quick scan of the site gives me:
this contact form to get more info:
https://escrow.com/contact/sales.asp
A FAQ:
https://www.escrow.com/support/faq/index.asp?sid=8
www.Transpact.com offers a similar but lower cost service.
It is also UK Government (FSA and HMRC) registered.
It offers a simple SOAP API for easy integration into your website.

Categories