Sending received original JSON via cURL - php

I just want a little help.
So this is the scenario:
I have a php script that received a JSON and get it by file_get_contents function.
The json is send by API more likely like Glabs.
So for example I'll text the given shortcode for the API and it will send in json format and I'll be receiving it via PHP.
My question is, how can i send the original json format that i've received via cURL?
and receive it again by file_get_contents?is it possible?
Here is my sample script:
$json = file_get_contents('php://input');
$json = stripslashes($json);
$values=json_decode($json,true);
$Msg=$values["inboundSMSMessageList"]["inboundSMSMessage"][0]["message"];
$dateTime=$values["inboundSMSMessageList"]["inboundSMSMessage"][0]["dateTime"];
$MPN=str_replace("tel:","",$values["inboundSMSMessageList"]["inboundSMSMessage"][0]["senderAddress"]);
That is my script for receiving a message sent from a mobile phone to a shortcode then received by my script.
Thanks for who will help me

Related

How to get webhook response data using tradingview and php

I'm trying to get Tradingview signals to my remote php script using webhook and trading view alerts. I can't succeed . I'm following this way
In my trading view strategy I have this
strategy.entry("Long", strategy.long, alert_message = "entry_long")
strategy.exit("Exit Long", "Long", limit=LONG_take_profit, stop=LONG_stop_loss, alert_message = "exit_long")
then I set the alert as follow
Then I set a PHP script as follow to receive the POST curl JSON data from Trading view
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fetch RAW input
$json = file_get_contents('php://input');
// decode json
$object = json_decode($json);
// expecting valid json
if (json_last_error() !== JSON_ERROR_NONE) {
die(header('HTTP/1.0 415 Unsupported Media Type'));
}
$servdate2 = time();
$servdate=date('d-M-Y H:i:s',$servdate2);
file_put_contents("/home/user/public_html/data.txt", "$servdate :".print_r($object, true),FILE_APPEND);
}
I receive the alert via email correctly but I do not receive data in /home/user/public_html/data.txt . What am I doing wrong ? How to send the Tradingview JSON data to my remote PHP script ?
I'm not familiar with PHP, but you've asked:
How to send the Tradingview JSON data to my remote PHP script ?
Your alert message as it is currently written in the alert_message argument, is not valid JSON and therefore it is sent as a txt/plain content type. If you want the alert to be sent as application/json you will need to have it in a valid JSON.
Webhooks allow you to send a POST request to a certain URL every time the alert is triggered. This feature can be enabled when you create or edit an alert. Add the correct URL for your app and we will send a POST request as soon as the alert is triggered, with the alert message in the body of the request. If the alert message is valid JSON, we will send a request with an "application/json" content-type header. Otherwise, we will send "text/plain" as a content-type header.
You can read more about it here.
EDIT:
You can make minor adjustments to your code, and it will be sent as JSON:
strategy.entry("Long", strategy.long, alert_message = '{"message": "entry_long"}')
strategy.exit("Exit Long", "Long", limit=LONG_take_profit, stop=LONG_stop_loss, alert_message = '{"message": "exit_long"}')
Replace the script with:
<?php
file_put_contents("/home/user/public_html/data.txt", print_r([$_GET, $_POST, $_SERVER], true));
to better understand the incoming data.
Maybe the data are present in $_POST variable, if not, post the whole result here (via pastebin).
(also make sure the PHP script starts with <?php)

Display JSON data from POST request in PHP

I have an apache2 server and php file to receive json via POST in my php code like this
$json = json_decode(file_get_contents('php://input'), true);
$mac_ = $json["code"];
$userId_ = $json["msg"];
and saving it to mysql. This work when I send post via python's requests but not when I'm recieving data from API android device. - When I look at the apache log file, there is a connection, so I'm assuming that some data are received, but because I can't touch that API android device I'm not sure if this part $json["code"] has a right parameter. I would like to know a way to save somewhere all recieved data from this part
$json = json_decode(file_get_contents('php://input'), true);
because I can't check it by echo or so

Messenger PHP chatbot receives more JSONs

my PHP chatbot receives multiple JSONs with php://input. It gets input message (that's correct), but then it receives JSON with message sent by bot (response), delivery confirmation and read confirmation.
How can I keep only that first JSON?
I need it because it then has problem when I send an attachment to my bot, it processes wrong JSON and I cannot get URL from it.
I have this code for handling input:
$input = json_decode(file_get_contents('php://input'), true);
file_put_contents('idkResponse.txt', file_get_contents('php://input') . PHP_EOL, FILE_APPEND);
And I'm getting all these JSONs
{"object":"page","entry":[{"id":"XXXXX","time":1496729805870,"messaging":[{"sender":{"id":"XXXXX"},"recipient":{"id":"XXXXX"},"timestamp":1496729805429,"message":{"mid":"mid.$XXXXX","seq":1459814,"text":"Hey"}}]}]}
{"object":"page","entry":[{"id":"XXXXX","time":1496729806679,"messaging":[{"sender":{"id":"XXXXX"},"recipient":{"id":"XXXXX"},"timestamp":1496729806556,"message":{"is_echo":true,"app_id":XXXXX,"mid":"mid.$XXXXX","seq":1459815,"text":"Wut?"}}]}]}
{"object":"page","entry":[{"id":"XXXXX","time":1496729806873,"messaging":[{"sender":{"id":"XXXXX"},"recipient":{"id":"XXXXX"},"timestamp":1496729806873,"delivery":{"mids":["mid.$XXXXX"],"watermark":XXXXX,"seq":0}}]}]}
{"object":"page","entry":[{"id":"XXXXX","time":1496729808233,"messaging":[{"sender":{"id":"XXXXX"},"recipient":{"id":"XXXXX"},"timestamp":1496729808232,"read":{"watermark":XXXXX,"seq":0}}]}]}
Thank you

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

Get and Send file with PHP and RESTful API

One of my partners has an API service which should send an HTTP POST request whenever a new file is published. This requires me to have an api file which will get the POST this way:
http://myip:port/api/ReciveFile
and requesting that the JSON format request should be:
{
"FILE ":"filename.zip",
"FILE_ID":"123",
"FILE_DESC":"PRUPOUS_FILE",
"EXTRAVAR":"",
"EXTRAVAR2":"",
"USERID":" xxxxxxxxxxxx",
"PASSWORD":"yyyyyyyyyyy"
}
Meanwhile it should issue a response, in JSON format if it got the file or not
{"RESULT_CODE":0,"RESULT_DESCR":""}
{"RESULT_CODE":1001,"RESULT_DESCR":"Bad request"}
And after, when I am finished elaborating the file, I should send back the modified file same way.
The question is, now basically from what I understand he will send me the variables witch I have to read, download the file, and send a response back.
I am not really sure how to do this any sample code would be welcomed!
I'm not sure exactly what the question is, but if you mean creating a success response in JSON for if an action occurred while adding data to it which is what can be understood from the question, just create an array with the values you wish to send back to the provider and do json_encode on the array which should create json and just print it back as a response.
About receiving the information; all you have to do is use the integrated curl functions or use a wrapper (Guzzle, etc) to output the raw JSON or json_decode data into a variable and do whatever you please with it.
From what I read in the question, you wish to modify the contents of it. This can be achieved by just decoding the json and changing the variables in the array, then printing the modified JSON back as a response.
Example (this uses GuzzleHTTP as an example):
$res = $client->request('GET', 'url');
$json = $res->getBody();
$array = json_decode($json, 1); // decode the json
// Start modifying the values or adding
$array['value_to_modify'] = $whatever;
$filename = $array['filename']; // get filename
// make a new request
$res = $client->request('GET', 'url/'.$filename);
// get the body of specified filename
$body = $res->getBody();
// Return the array.
echo json_encode($body);
References:
http://docs.guzzlephp.org/en/latest/

Categories