cURL feedback with some value (true/false) - php

So I was wondering if it is possible got get a value back as feedback after sending a cURL request. I got a project going on where I send XML data to a Press office, where they check if the data is valid and the press process can begin. Now I need some feedback. I know I can get a message back when i do -
echo 'not enough paper'
or echo 'we can start'
on that page (I mean if they do). But I need a value to like true or false, so I can set a status on my side. I will keep googling and just leave this question here, maybe some one got an idea.

If you are sending some data to an external service or URL, only they can decide to put a status message in the response.
I assume you are getting an HTTP 200 code which indicates that the request was successful, but what the other side does with the request is totally up to them. You might have to request that they add some additional parameters to their response.
Depending on what you ask them to do, you would parse their response that you get when you execute the cURL command -
$retValue = curl_exec($request);
// if they return XML data
$retXML = new SimpleXMLElement($retValue);
// if they return JSON data
$retJSON = json_decode($retValue);
You should use what ever method you feel more comfortable with. If you are already dealing with XML data within your code, then just request that they return some XML data with a status message.
Example return data -
XML
<response>
<status value="true" />
<message value="We can start!" />
</response>
JSON
{ 'status':'false','message':'not enough paper!' }

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)

Processing POST data from a third party

I have been struggling with the following problem for few days. I am expected to receive POST data from a third party company for certain real time events. I have been in contact with them as to why my script isn't seeing anything. Unfortunately, they are less then helpful and just tell me "it works for everybody else".
I setup a simple PHP script after doing some research on the web but it always shows no post data. This isn't my area of expertise so I may be missing something obvious.
Here is the only documentation they give:
This API will send a real-time http request upon every successful event with all of the conversion data. The data is sent via an http POST method, and the data is JSON formatted.
This is my script which for now is trying to just log it to a file. The file is created and I see the IP address of the request but the output is empty in terms of post data.
ob_start();
echo "Request from :" . $_SERVER[REMOTE_ADDR];
echo "print_r:".print_r($_POST,true);
if(array_key_exists('app_id', $_POST))// me attempting to access a specific key they claim is in the post data
{
echo "app id = " . $_POST['app_id'];
}
//I also tried both of these and neither output anything
//foreach ($_POST as $key => $value) //idea 1
foreach($_POST as $item) //idea 2
{
//echo "key=".$key." value=".$value; //idea 1 log
echo "next=";//idea 2 log
echo $item;
}
$contents = ob_get_flush();
file_put_contents("log.txt",$contents,FILE_APPEND);
There's not a lot to go on here -- who knows what the client is actually sending -- but here's a thought:
POST is just an HTTP command. It's traditional for the body of a POST to be a series of key-value pairs from a form, but it is not actually necessary. It's possible that the remote client is issuing a POST request to your server and then just delivering a JSON blob in the request body, which would not be successfully parsed into the $_POST array.
I recommend exploring the answer at How to get body of a POST in php? to see if that helps shed light on this problem.

sending a response from a php script to iOS

I am creating an iPhone app which sends a username and password to a php script, the php script then looks in a mySQL database for the values and sets a boolean to either 0 or 1, depending on whether or not the user should be authenticated. I really have no idea where to start or even what I should Google to look into how to do this.
Is this feasible?
Is this the proper way to authenticate a user in an iOS app?
Thanks!
There are various types to achieve this.
a) Generate an XML or JSON file in PHP, and read the content back in iOS. (this method gives you the benefit of fetching any extra data if you want).
b) Send back HTTP header() from PHP, and read the HTTP response code. you can do something like this.
function checkLogin()
{
//Check login
if($login == true) {
header('HTTP/1.1 200 OK');
} else {
header('HTTP/1.1 401 Unauthorized');
}
}
c) You can output anything in PHP(plain text, JSON, HTML etc.), as the output generated by PHP will be received as HTTP response.
Anything the PHP script outputs will be returned as the HTTP response. Simply output something meaningful, and read it in the client.
The simplest solution would be to use HTTP status codes. Then you don't even have to care about the response body.
If authenticated: "HTTP 200 OK"
If unauthorized: "HTTP 401 Unauthorized"
Resource: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
You can write a php script like this:
<?php
// the authentication procedures memorized in the $authentication variable the result of authentication process. Supposed to be 1 if successful
echo $authentication;
?>
Call this script from your iOS by using an NSURLRequest object for example.
P.S.: However, for data exchange between the client and the server you should use the JSON format.

Getting empty result within PHP when in browser I get the JSON exception

I have this code:
$url="https://graph.facebook.com/me/friends?access_token=".$access_token."&fields=id,first_name,last_name&limit=10";
$content=file_get_contents($url);
Whenever I use this on a non authenticated user I should get feedback of OAuthException, which doesn't show up in the PHP the $content is empty. While if I copy the URL to the browser I get the result and I see the exception.
I want to detect if the user is logged in and the session data is valid.
What might be wrong?
Maybe Facebook decides whether to respond with exception feedback or with just no response depending on the contents of Accept HTTP header(s) you are sending (file_get_contents sends different HTTP headers than your browser).

Get SOAP XML before __soapCall? [duplicate]

This question already has answers here:
Inspect XML created by PHP SoapClient call before/without sending the request
(3 answers)
Closed 1 year ago.
Is it possible to get the XML generated by SOAP Client before sending it to webservice?
I need this because response from webservice if one of parameters is really wrong I get errors like
Server was unable to read request.
---> There is an error in XML document (2, 408).
---> Input string was not in a correct format.
This typically includes firing up tcpmon or some other tcp watcher utility, capturing the webservice call, copy and paste xml to text editor and go to column 408 to see what's the problem.
I'd really like to simplify this process by getting the XML before sending it.
It is very, very hard (nigh impossible) to do that. What is much easier is using the SoapClient class' built-in debugging functionality to output the request after it has been sent. You can do that like so:
First, when creating your SOAPClient, enable tracing, like so:
$client = new SoapClient($wsdl, array('trace' => true));
Then do whatever processing is necessary to get ready to make the SOAP call and make it. Once it has been made, the following will give you the request you have just sent:
echo("<pre>"); //to format it legibly on your screen
var_dump($client->__getLastRequestHeaders()); //the headers of your last request
var_dump($client->__getLastRequest()); //your last request
And, if you want to see the response as well, the following should work:
var_dump($client->__getLastResponseHeaders()); //response headers
var_dump($client->__getLastResponse()); //the response

Categories