HTTP Responses, Webhooks and Json - php

I have been asked to write a program that takes a list of numbers and sends a post to ms.4url.eu via JSON/HTTP Post in format:
{
"username":"a",
"password":"b",
"msisdn":"071231231234",
"webhook":"http://example.com"
}
it receives a JSON Response,
{
"status":"ok",
"id":"1234-1234-12344423-123123"
}
I have been told I can use ngrok for the webhook and I have to send a HTTP Response 200 within 1s.
I should receive a Webhook Response:
{
"id":"1234-1234-12344423-123123",
"msisdn":"071231231234",
"status":"unavaliable",
"error":"1b",
"errorDesc":"Abscent Subscriber"
}
How would I go about grabbing the data from the JSON response and Responding with a HTTP 200 in order to receive the second response with the data?
I can get the first response in curl but I am unable to get the webhook working to a php file using ngrok and HTTP response sent to request the main information in the second response.
Edited :
I have executed the curl command,
curl -H 'content-type: application/json' \
-d '{"username":"a", "password":"b", "msisdn":"07123123124","webhook":"http://example.com/"}' \
HTTPS://ms.4url.eu/lookup
of which I get the first response "status ok". I would like to know how to get the response(Json format) in php using http post to the URL and the using a webhook to respond with 1second with a http 200 response to receive the further information from the API URL.

I ended up using ngrok and viewing the Raw POST response and getting the JSON and viewing the Raw data I still had more code to do in order t make this question valid as there are too many points to answer.

Related

FacebookMessenger API send messages but don't get

I'm currently developing a messenger BOT where the BOT is on an server A hidden with a vpn.
So I have a server B which gets the request and sends it to the server A.
The problem is that I'm able to send messages and quick replies with A when I send an http request directly. But A never get curl request from B.
The second problem and the main, is that the first server don't 'always' get the request from the Facebook server.
In my code I copy past the content of php://input to text.txt on both server A and B to investigate
if (($response = file_get_contents("php://input")) === FALSE) {
file_put_contents("text.txt", "error get");return false;
}
if (($json = json_encode($response)) === FALSE) {
file_put_contents("text.txt", "decode");return false;
}
file_put_contents("text.txt", $json);
when I do curl -X POST -H "Content-Type: application/json" -d '{"name": "linuxize", "email": "linuxize#example.com"}' myserver.com I always get the data on server B
But when I send message on messenger, and that I should get an HTTP request to B, I don't get the request OR I get an older request which is always with the same content except the "time". But the timestamp is the same.
If anyone understand/know why I don't always get the right data in php://input it would be awesome

Wordpress receiving a payload via a response url from third party API

I am sending & receiving JSON responses from a third party API, in one instance though they need to send a response to a url on my site ie: https://my-wordpress-site/api/confirm?transaction=T0efewgfweg78105 Can someone point me in the right direction on how to receive the payload from this response? The payload will be a JSON response like: “Status”: "Success" What script will I run on https://my-wordpress-site/api/confirm.php
If I understand the situation correctly
You can fetch this data with this snippet:
if(isset($_REQUEST['transaction']) && !empty($_REQUEST['transaction']))
{
$response_body = file_get_contents('php://input');
$data = json_decode($response_body);
}
php://input is a read-only stream that allows you to read raw data from the request body

php http post response for web hook

I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman

Catch POST response from API with 302 status

I am trying to catch POST response send to me by external API.
The problem is that POST array is completely empty while I can check in firebug that browser recieved it but with codes 302 FOUND and second (with same body) with code 307 TEMPORARY REDIRECT:
Is there any way to grab this data inside my script or is this something wrong with server re-directions?
If you are using the CURL library, there are two options that help with your case:
curl_setopt($curl,CURLOPT_HEADER,1);
This returns the response header including the status code. You can see whether 302 is returned.
Or you can simply follow the redirect
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
Edit: sorry just saw you were doing this on the client side.
If this is an AJAX call, you can get the status code in the raw XHR object.

How to stimulate cURL request to a request using postman

I am using the following cURL request to localhost which runs fine:
curl -u admin:e4d4face52f2e3dc22b43b2145ed7c58ce66e26b384d73592c -d "{\"jsonrpc\": \"2.0\", \"method\": \"feed.list\", \"id\": 1}" http://localhost/minifluxR/jsonrpc.php
But when I send the same request using Postman instead of cURL, I am getting:
{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}
In Postman I used a GET request and sent the following as headers:
url:http://localhost/minifluxR/jsonrpc.php
username:admin
api_token:e4d4face52f2e3dc22b43b2145ed7c58ce66e26b384d73592c
method: feed.list
The following is the PHP function I am trying to trigger:
$server = new Server;
$server->authentication(array(
\Model\Config\get('username') => \Model\Config\get('api_token')
));
// Get all feeds
$server->register('feed.list', function () {
return Model\Feed\get_all();
});
Please help me to correct these errors.
When using cURL, the -u option (or --user) is used to supply the credentials for HTTP Basic authentication. This sets the Authorization header to contain the necessary data to authenticate with the server.
These steps apply to Postman's packaged app. For steps for the legacy app, view this of revision this answer.
To use HTTP Basic authentication as you were in your cURL command, click the Authorization tab and enter your credentials. Clicking Update Request will add the necessary Authorization header for you.
To submit the JSON data in the same way that you did with cURL, use a POST request, select raw under the Body tab, and enter your data like so:
To debug this I used Fiddler - a free web debugging proxy.
I used cURL's --proxy option to make it send its requests through Fiddler like so:
curl \
--proxy http://localhost:8888 \
-u foo:bar \
-d "{\"jsonrpc\": \"2.0\", \"method\": \"feed.list\", \"id\": 1}" \
http://localhost
Now that the request goes through Fiddler, I can select it from the session list, and use the "raw" inspector to see the raw request:
This shows me that the cURL is making a POST request with HTTP Basic authentication and application/x-www-form-urlencoded content. This type of data normally consists of keys and values, such as foo=bar&hoge=fuga. However, this cURL request is submitting a key without a value. A call to var_dump($_POST) will yield the following:
With a = at the end of the data (like so: {"jsonrpc": "2.0", "method": "feed.list", "id": 1}=) the var_dump will yield the following:
However, it seems that JsonRPC will use file_get_contents('php://input') in your case. This returns the data that was submitted with the request, including a =, if the data ends with it. Because it will try to parse the input data as a JSON string, it will fail if the string ends with a =, because that would be invalid JSON.
Using the FoxyProxy extension for Chrome, I created a proxy configuration for Fiddler (127.0.0.1:8888), which allowed me to easily debug the data being sent by Postman's POST request. Using x-www-form-urlencoded with a key of foo with no value, the data sent was actually foo=, which would result in your JSON string being invalid.
However, using "raw" input will allow for the specified data to be sent without a = being added to the end of it, thus ensuring the data is valid JSON.
Curl is using HTTP Basic authentication by default. Your headers set in Postman are something different. Try using Basic Auth in Postman. It is in top panel, you fill in username and password and authorization header will be generated.

Categories