简体   繁体   English

AWS Lambda-通过直接的PHP curl调用

[英]AWS Lambda - Invoke by straight PHP curl

Alright, 好的,

So I'm aware of the AWS PHP SDK. 因此,我知道了AWS PHP SDK。 I do not want to use it to make a curl call to invoke a lambda function. 不想用它来制作卷曲要进行的调用lambda函数。

I've found things about using API Gateway. 我发现了有关使用API​​网关的信息。 I also do not want to use API Gateway. 我也不想使用API网关。 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. 我希望它尽可能地简单,只需一个直接的curl调用即可调用该函数。 As far as I understand it's what is happening in the SDK anyway, so it must be possible without using the SDK. 据我了解,无论如何这就是SDK中正在发生的事情,因此在不使用SDK的情况下必须能够实现。

I know the most sensible way would be to use the SDK, and I have used it successfully before. 我知道最明智的方法是使用SDK,而我之前已经成功使用过它。 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. 我遇到了一个不幸的情况,尝试将自定义业务逻辑添加到CMS的过时的蜘蛛网中,并且该SDK已包含在插件中,我不能简单地在自定义函数中重用已经命名的SDK 。

tldr; 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? 在Wordpress中编写publish_post钩子,无法使用AWS开发工具包,因为插件已在使用它,只是想进行直接的curl调用以调用该函数,而不打算使用API​​ Gateway,该怎么做?

****** UPDATE ****** ******更新****

Solved my problem, hanging head low... the reason I couldn't use the already loaded SDK was a versioning issue ... d'oh. 解决了我的问题,垂头丧气...我无法使用已经加载的SDK的原因是版本问题 ... 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! 希望听到解决方案!

Using AWS API Gateway is dead simple to link it to your Lambda function. 使用AWS API Gateway将其链接到Lambda函数非常简单。 You simply create a resource and stage, associate it to your Lambda function, and then deploy the API. 您只需创建一个资源并创建阶段,将其与Lambda函数相关联,然后部署API。 From there, you'll receive a public URL to send requests to that will execute your Lambda function. 从那里,您将收到一个公共URL,向该URL发送请求,以执行Lambda函数。

To my knowledge, this is the only way to execute a Lambda function over HTTP(S) 据我所知,这是通过HTTP(S)执行Lambda函数的唯一方法

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM