I am trying to get data from a webpage which shows most of the time just the value 0 but it sometimes changes to something like this.
{"deals":[{"symbol":"SymbolTest","type":"TestCall","amount":"0.00","duration":"30","durationUnit":"m","date":"2017.07.12 7:55:10 PM"}]}
But this text is only shown there instead of 0 for approx. 1-2 seconds.
So I created a script which checks the webpage constantly if the value changed from 0 to something else. And if another text is shown i will get a notification via IFTTT on my phone with the important data I need. (Theoretically) Thats where I am stuck. I think I got everything correctly but I just can not find my mistake.
This is my current code:
function sendNotification($url, $fields) {
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
$url = 'http://example.com:54321/?request=deals&token=example';
$notifyUrl = 'https://maker.ifttt.com/trigger/dealreceived/with/key/examplekey';
while (true) {
$content = file_get_contents($url);
if ($content != '0') {
echo 'No 0 found -IFTTT request sent';
$data = json_decode($content);
$notificationData = [];
foreach ($data->deals as $deal) {
$notificationData = [
'value1' => $deal->symbol,
'value2' => $deal->type,
'value3' => $deal->duration
];
sendNotification($notifyUrl, $notificationData);
}
}
}
The error I am getting is the following:
PHP Notice: Trying to get property of non-object in /home/example.cloudwaysapps.com/example/public_html/watch.php on line 36
PHP Warning: Invalid argument supplied for foreach() in /home/example.cloudwaysapps.com/example/public_html/watch.php on line 36
This is line 36:
foreach ($data->deals as $deal) {
Any advice is appreciated. I am stuck unfortunately..