How to create a Webhook server like Telegram Webhook - php

I need to create a Webhook server like Telegram Webhook server.
I googled it but didn't find any resources!
I'm not talking about receiving Webhook requests. I'm talking about creating a complete Webhook server to send HTTP POST requests to specific URLs. And our clients could receive the requests in their URLs by :
$response = file_get_contents('php://input');
Any helps would be great appreciated.
P.S:
Sorry for my bad English.

you can try Captain Hook laravel package, which provides you to add webhook to your laravel application

What a webhook actually does is nothing more than sending a request. The most easy way to set this up is by using Guzzle (https://packagist.org/packages/guzzlehttp/guzzle).
What you need to set up is on your side a script which decides what url to call, when that happens, just create the post request via guzzle.
$postData = [];
$client = new GuzzleHttp\Client();
$response = $client->request('POST', $url, $postData);

Related

Laravel - redirect request to external url and return its response as response

i'm building an app using Laravel for providing some API for an android application, and i'm using some external APIs from another server (with another URL). i want to make something like proxy or tunnel for external API requests from android side but in my own URL.
for example:
android wants to request for externalUrl.com/api/objects but i want he request to this myDOmain.com/api/x/objects and get the exact same response that the first link returns, without any change.
and there is more than one external API, and i don't want to write separate code for each one.
need something like this :
Route::any('/x/{somewhere}', function($request){
return [$request, externalUrl.com/api/{somewhere}]->response;
})
i'm not asking for http request libraries! i want to redirect request to another domain and return its request.
Best option is to install Guzzle. https://github.com/guzzle/guzzle
It's really simple to use it.
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'externalUrl.com/api/{somewhere}');
return $response->getBody();
You can use Guzzle :
use Guzzle\Http\Client;
use Guzzle\Stream\PhpStreamRequestFactory;
[...]
$request = new Client("externalUrl.com/api/{$somewhere}");
$response = $request->send();
return $response->getBody();

Guzzle Using Proxy

References:
http://docs.guzzlephp.org/en/stable/request-options.html#proxy
Set proxy in Guzzle
Env :
GuzzleHttp/6.2.1
curl/7.47.0
PHP/7.1.3-3+deb.sury.org~xenial+1
I am trying to use proxy server with async Guzzle calls.
I have discovered that when I set proxy when creating a client, it works.
e.g.
new Client(['proxy' => 'tcp://64.140.159.209:80'])
However when create a client with no options .. and then set proxy on Request, proxy is not used at all, and guzzle makes a direct connection from client machine to server machine. That is confirmed by hitting http://httpbin.org/ip and inspecting the Origin returned by httpbin.
I need the ability to set proxy on each request.
Here is relevant code:
$client = new Client();
$request = new Request(
'GET',
'http://httpbin.org/ip',
['proxy' => 'tcp://64.140.159.209:80']
);
$client->sendAsync($request)
->then(
...closure here
// process here
);
Hope this helps for someone.
The document http://docs.guzzlephp.org/en/stable/request-options.html#proxy only lists creating a new request from a client.
That means I understood the usage wrong. I was creating new Request directly and passing third param with proxy info expecting that to be changed per request within a single client. It looks like that proxy is set on per client basis, even if you are making asynchronous calls.
So I had to modify my application to use a new client per async request.

Slim v3 how to send a response to a different URL / Ip address?

i try to implement the paypal IPN with slim.
see code here
they use curl to make a post request to an URL. Is it not possible to just change the URL in the slim response object?
other ways to send requests to somewhere with slim?
EDIT
I am talking about this line:
$res = curl_exec($ch);
what is the equivalent slim way to send a request to some url?
You can send PSR7 Requests with the help of HTTPlug.
You have to create some Adapter classes to tell HTTPlug how to create Slims Request and Response objects. In your code you just create your Slim Request and call the Client with this. You can choose between some clients, for example Curl or Socket.
If you want to send your user to PayPal in order for them to pay, then you need to create a form on your website which POSTs to PayPal directly.
The code you linked to is a POST request from PayPal back to your server. To handle that in Slim, you create a post route:
$app->post('/paypal-ipn', PayPalIpnAction::class);
Within your Action class you need to send POST request back to PayPal:
class PayPalIpnAction
{
public function __invoke($request, $response, $args)
{
$dataFromPaypal = $reqest->getParsedBody();
// Validate data from PayPal using HTTPlug, Guzzle or
// you use can the PayPal example code directly.
// If data is valid, process data and do whatever you
// need to with it.
// All done. Return the response.
return $response;
}
}
Note that the verification step back to PayPal isn't directly related to the handling of the notification in Slim as it's part of your code for handling the notification.

Guzzle asynch request not working

I'm using Guzzle that I installed via composer and failing to do something relatively straightforward.
I might be misunderstanding the documentation but essentially what I'm wanting to do is run a POST request to a server and continue executing code without waiting for a response. Here's what I have :
$client = new \GuzzleHttp\Client(/*baseUrl, and auth credentials here*/);
$client->post('runtime/process-instances', [
'future'=>true,
'json'=> $data // is an array
]);
die("I'm done with the call");
Now lets say the runtime/process-instances runs for about 5mn, I will not get the die message before those 5mn are up... When instead I want it right after the message is sent to the server.
Now I don't have access to the server so I can't have the server respond before running the execution. I just need to ignore the response.
Any help is appreciated.
Things I've tried:
$client->post(/*blabla*/)->then(function ($response) {});
It is not possible in Guzzle to send a request and immediately exit. Asynchronous requests require that you wait for them to complete. If you do not, the request will not get sent.
Also note that you are using post instead of postAsync, the former is a synchronous (blocking) request. To asynchronously send a post request, use the latter. In your code example, by changing post to postAsync the process will exit before the request is complete, but the target will not receive that request.
Have you tried setting a low timeout?

Guzzle Maintaining Cookies

I'm creating an API for a site that allows the user to login via the API. I'm using Guzzle, but the question is how do I use the Cookies Plugin with Guzzle? In cURL I can use a cookie file, and pass this along with requests. But the example on the Guzzle docs looks confusing.
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
$cookiePlugin = new CookiePlugin(new ArrayCookieJar());
// Add the cookie plugin to a client
$client = new Client('http://www.test.com/');
$client->addSubscriber($cookiePlugin);
// Send the request with no cookies and parse the returned cookies
$client->get('http://www.yahoo.com/')->send();
// Send the request again, noticing that cookies are being sent
$request = $client->get('http://www.yahoo.com/');
$request->send();
echo $request;
It seems to be making 3 requests. I don't understand why it's making a request to test.com, then twice to yahoo.com. Why are you not able to make 1 request?
It's just an example... You don't have to make three requests. Just attach the cookie plugin to your client and you're good.

Categories