How to test integrations between local environment and external services (webhooks) - php

since i started to develop web applications i always found very annoying to inspect external services requests and test my application with "realistic" scenarios. What's the solutions we've got to expose my local development environment to external services?
I am using Laravel Homestead and/or PHP Development Server

Before some time searching for the definitive answer i found a solution that solves all these problems in an efficient, free way, this is Ngrok, a product created by Alan Shreve -- which has worked on giants like Microsoft and Twilio. Alan wrote about the creation of Ngrok and an article on his blog he describes the product as:
"Ngrok is a tunneling, reverse proxy that establishes secure tunnels from a public endpoint to a locally running network service while capturing all traffic for inspection and replay. It is an open-source project on GitHub."
Let's get started:
Well, now that you know a little bit about the tool and why i found it let's demonstrate how to expose a local environment to allow third-party services to submit requests to local environments via Webhook is very simple and the two solutions that I'm going to present have been tested and work with the following scenarios :
Scenario 1: A PHP project using PHP 7.2.6 Development Server
Scenario 2: A Laravel (PHP) project using Laravel Development Server (php artisan serve)
Scenario 3: A Laravel (PHP) project using Laravel Homestead
To do this just follow the steps:
Access the Ngrok website
Register or Login
Download the client based on your OS
Authenticate the client downloaded following the instructions that appear on the same page you've downloaded it.
For the first two scenarios just run the following command
ngrok http <host>:<port>
ex: ngrok http 127.0.0.0.1:666
For the third scenario (if you are using a domain to access the homestead Ex: homestead.test you must rewrite the host-header. But don't worry, to achieve it just increment the command above to something like)
ngrok http <homestead_host_ip>:<port> -host-header=<homestead_domain>
example: ngrok http 192.168.10.10:80 -host-header=homestead.test
Voilà, now just point the URL generated by Ngrok in Webhook that you want to test and enjoy this wonderful solution.

Related

Xero testing webhooks on localhost

So I am running a PHP application on localhost and I want to test an incoming web hook. Is there a recommended way to test the local development for this?
Is there an example web hook invoice-paid POST request I could just test with something like postman on my localhost?
You can use a tool like ngrok or Pagekite to expose your local server. You download and run a program on your machine and provide it the port of a network service, usually a web server.
It connects to the ngrok cloud service which accepts traffic on a public address and relays that traffic through to the ngrok process running on your machine and then on to the local address you specified.
I wrote a blog post on the topic
https://devblog.xero.com/lets-play-web-hooky-with-php-34a141dcac0a
You can use ngrok for test webhook in your local machine. Here is an additional detail to #sidney.maestre answer,
You only have to use Xero demo company in developer console for once,
then you can use the "Replay" button to use exact request every time in ngrok's dashboard.

Google Cloud Platform : How to develop on my local environment with GCP resources

I'm using Google App Engine Flex to develop an angularjs/php-rest backend application.
I've a successful port from regular servers to AppEngine, and I now want to integrate more with GCP services like : StackDriver, Cloud Storage and so on.
StackDriver to have logging & monitoring.
Cloud Storage: to store export data files and zip them before sending it to browser.
My question is how do I develop locally on my laptop (which can be online & offline) ?
I didn't find in the documentation "the way" of local development :
Should stackDriver or Cloud Storage client be configured to write on disk instead of reaching GCP ?
Should I configure some proxy (like the cloud_sql_proxy) to be able to reach GCP ? Should I create a project for my local dev ? How does it work if I'm offline ?
Any hint appreciated :)
App Engine Flexible doesn't come with a development server or service emulators for use during development so you may use the services directly.
Stackdriver Logging: logs written to stdout and stderr are automatically sent to Stackdriver Logging for you, without needing to use Stackdriver Logging library for PHP. This may be enough for you to get logs locally but we recommend that you use the PSR-3 logger which automatically adds metadata to your logs so that your application logs are correlated to the request logs. You can set it up to run locally and log to your project by following the doc here.
Stackdriver Monitoring: Google App Engine includes built-in support for Monitoring in the flexible environment (when deployed) and doesn't require configuration. The monitoring agent cannot be installed on your local machine though, but it would be pointless to monitor it anyway.
Cloud Storage: an easy option is to create a dev bucket that you can use during development. You can create it in whichever project you wish and grant permissions to your development service account.
One common practice is to create different GCP projects for prod, staging and dev purposes. This allows you to create specific resources for a given environment. Taking logging as example, you'll be able to see logs and troubleshoot any issue with it within the dev project, without polluting your prod project's logs. That'd be true with CloudSQL, Datastore, etc...
You don't need to configure any proxy for those services. The cloud_sql_proxy is a convenient method to enforce secure connections and ease authentication with CloudSQL instances without the need to whitelist IP addresses.
Regarding the offline situation now, of course those calls from your local app to those services will fail if you don't have internet connection at that time (intermittent disconnections may actually help you to test your retries and error handling mechanisms). If you expect to develop with no internet connection at all though, you'll need to write stub services to mimic the expected behavior locally.

automatic pull on production server after push to BitBucket

I develop my code locally, commit it to my local repsoitory and then push it to my Bitbucket server. After that I have to login via ssh to my production server
like this:
ssh my-server#my-host.de
pass: very-secure-passs
cd www/myPage
cd git pull origin master
pass: very-secure-pass
I would like to avoid login to my production server and let him pull automatically.
I want to have 3 repositories (local, BitBucket and production Server), so I cant use this solution: Do an automatic pull request after pushing to server
I found this question A hook that let `pull` from VPS when I `push` to Bitbucket which is exactly what I want, but it is from 2013 and the answer is outdated since BitBucket has changed since then.
I found here https://community.atlassian.com/t5/Answers-Developer-Questions/How-can-I-deploy-my-bitbucket-repo-to-my-production-server/qaq-p/565348 that someone suggested to use a free Plugin called HTTP-Request Hook for Bitbucket Server
set up an automated "Pull": Each time you do a push to your central
repository, your production machine is notified and pulls the
repository on notification. Bitbucket Server offers serval plugins to
support the notification process - the one I use is Http Request Post
Receive Hook: each time a push is made, a configured URL is contacted,
submitting some info. On my production machine I have set up a little
web server, waiting for this HTTP-Request. On receiving the
HTPP-Request I evaluate the given parameters and perform an action
(for example: pulling the repository ...)
Now my questions are:
Is it possible to use a Webhook instead of the HTTP-Request
Hook Plugin?
How should the file on my production server look like so that it will do a pull request when it receives a HTTP-Request? I would be interested in a basic example in PHP.
The webhook documentation you linked is for Bitbucket Cloud (bitbucket.org), not Bitbucket Server (which is self-hosted and has some other URL). If you're using BB Cloud, then the HTTP-Request Hook Plugin won't work, but the documentation you linked will. If you're using BB Server, then you can use https://confluence.atlassian.com/bitbucketserver/managing-webhooks-in-bitbucket-server-938025878.html instead to define a webhook.
For the second half of your question - how to set up your server's end of a webhook - you'll need to have a small service that listens for the incoming webhook, does whatever authentication you want, and then runs your pull method. There are a zillion ways to do this, but most will vary based on your preferred language and security settings and on the network configuration of the server in question. I'd suggest a Google search for "webhook deploy $LANGUAGE" to see how some others have done it with your preferred language, or to see if there's a public repo or gist or snippet out there that you can use.
Instead of directly trying to pull on production server, you can setup a Jenkins job that will push the code to production server on each commit. Using Jenkins you can even customize the solutions to match particular token in commit message.

Web Hosting on Amazon AWS (PHP + MySQL)

I am totally confused on how to host a Dynamic website created using PHP and MySQL in Amazon Cloud.
I went through Amazon S3 and I hosted a static website there!
Then I tried Amazon EC2 and I learned some aspects about the concept of VPC. I thought that the dynamic websites are hosting in Amazon Cloud using EC2. I followed some steps and they taught me how to launch a website using Drupal (But, I didn't want that !! )
No other tutorials on EC2 to deploy my web application was not found.
Then I found AWS Elastic Beanstalk, I uploaded a simple PHP document and I can see that deployed successfully.
But Still, I am not satisfied. Because, I don't know which is the correct way to deploy my PHP application.
So can anyone direct me on Deploying a PHP MySQL Application in AWS ?
Depends on your needs. Elastic Beanstalk might be a good option for many apps, but I chose EC2 for my app's backend (using PHP, MySQL and S3 for storage).
Quick steps to get you up and running:
Log into the AWS Mangement Console and start a new EC instance (Windows server 2012 R2 Base > t2.micro should be good enough for a start!)
At step "6. Configure Security Group", add Rules for at least HTTP, HTTPS and RDP (so you can connnect via Remote Desktop)
Connect to your new instance via Remote Desktop and install a decent browser (Enable File Downloads in IE's Security Settings and download Chrome or Firefox)
Open the Windows Firewall and add rules for the same ports you opened in the Security Group of your Instance in the AWS Management Console. (Right-click on “Inbound Rules”, then select “New Rule…”)
Download and install XAMPP (I put it in C:\xampp)
Open the XAMPP Control panel and install Apache and MySQL as services (so they will start automatically when your instance launches); make sure everything is started up.
Now put your files in C:\xampp\htdocs\ and you're ready to go!
Bonus Steps:
Set up Filezilla FTP Server (and open the required ports in both the instance's security group and the Windows Firewall) so you can upload/download files without having to go through Remote Desktop.
Get an Elastic IP and assign it to your instance, so it's IP address will never change.
Get an SSL certificate so you can use HTTPS
The answer depends on the load that you are expecting and the resources you have to handle all the administration tasks.
If you expect heavy or variable loads, there are many reasons why not to deploy a production PHP + MySQL application on a EC2.
Here are some of the benefits of deploying to Elastic Beanstalk instead of a manual configured EC2:
You get version control of each deployment.
You can scale up or down automatically if you need more/less instances to handle new load.
You get a load-balancer in front of your EC2s instances with a bunch of out-of-the-box "recommended" configurations.
Regarding MySQL, if you go for an Amazon RDS instance you can handle replication, monitorization and automatic backups with pretty low effort. A lot of the configurations you would need to tweak are now available through parameter-groups.
On the other hand, if you want to have full control of everything that is going on on your server (that means you have time to monitor, backup and do maintenance tasks, which is not my case :), or if you do not plan to have much traffic, or if you want the less expensive option, you should go with a low cost EC2 instance.
In my experience, (after 2 years of working on AWS with 10 production applications, I'm kind of a regular AWS user) pretty much every customization or change I needed on both RDS and EBS I was able to tweak it and get it working, so I'm pretty satisfied with choosing the EBS+RDS option.
Below are two links i found which are helpful to Create and Update an Application with AWS Elastic Beanstalk
https://aws.amazon.com/getting-started/tutorials/launch-an-app/
https://aws.amazon.com/getting-started/tutorials/update-an-app/

Create and consume web service on local machine by using WAMP

Is it possible to create an environment on local machine( i.e. localhost) where we can create and consume web service using PHP?
Is there any flow to create a web service like environment on localhost?
I would like to create an environment on localhost for web services where local machines, on intranet, get connected to main server (here again it's local machine) and can consume web services.
Like on internet we have Web Servers from where we can create and consume web services, Can we do the same on localhost?
Edit:
I am using WAMP with
PHP 5.3 Apache 2.* MySQL 5Windows 8
It is easy to create a local web service. You will need a local web server (like IIS or xampp) to execute PHP locally.
Access you PHP application in your browser using http://localhost/<yourApp>.php and provide any webservice at a local URL as well, e.g. http://localhost/<yourService>.php. Your application can then access the webservice using the local URL.
This works both for REST or SOAP-style services.
EDIT: Your edit shows that you already set up a local environment to develop and test webservices. You should really be more specific in your question, maybe you are looking for a tutorial to create web services with PHP in genereal. If so, Google will be your best friend: https://www.google.de/#q=create+web+service+using+php+tutorial

Categories