I'm trying to receive a webhook from hipmob (live chat app) that should be triggered at a chat event.
Documentation: https://www.hipmob.com/documentation/chat-events.html
I've been following How to catch the HTTP POST request sent by a Shopify Webhook , and i have to say that i am completly new to this. I tried the solution from the first answer but with no success.
this is what i did:
<?php
$webhookContent = "";
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
$file = 'webhook.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
// Write the contents back to the file
file_put_contents($file, $current);
error_log($webhookContent);
?>
but with no success
I was able to solution it with:
<?php
$entityBody = file_get_contents('php://input');
$file = 'webhook.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
// Write the contents back to the file
file_put_contents($file, $entityBody);
error_log($webhookContent);
?>
Related
I am using the php post function to add data into an HTML file.
Every time i use it the data is added in the bottom of the page.
How can i make it to add it on the top..??
<?php
session_start();
date_default_timezone_set('Asia/Calcutta');
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$fp = fopen("log.html", 'a');
date_default_timezone_set("India/Kolkata");
fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
fclose($fp);
}
?>
I Am new in coding
You'll have to get the full content of your file, concatenate it with the new data at the beginning (new data + old data), and overwrite the file with this new content.
Here is the script normally. It saves data to a file.
<?php
if (isset($_GET['field'])) {
$file = 'data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= "\n".$_GET['field'];
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
But I want it to add (added by a user) at the end of what is sent. I have tried this:
<?php
if (isset($_GET['field'])) {
$file = 'data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= "\n".$_GET['field'] + "(added by a user)";
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
But it doesn't work. How do I do this?
My code:
$cc=file_get_contents('http://example.com');
file_put_contents('savefile.htm', $cc);
When I navigate to this address, my browser redirects to 'http://example.com/en/index.htm'.
But file_get_contents doesn't recdirect, what can I do?
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
Seems to be a known bug in PHP. See: https://bugs.php.net/bug.php?id=53018
But you can use cURL instead. See http://www.jonasjohn.de/snippets/php/curl-example.htm for some example.
Trying to fwrite the pdf results from PrinceXML to the server (in a new pdf file) instead of exporting via headers to acrobat. The below code displays the results on the browser.
require_once("../library/Prince/prince.php");
$princeSettings = $this->getInvokeArg('bootstrap') >getOption('prince');
$prince = new Prince($princeSettings['path']);
$prince->setHTML(true);
$result = $prince->convert_string_to_passthru($this->htmlView);
$fp = fopen("./files/reports/report.pdf", "w");
fwrite($fp, $result);
fclose($fp);
Figured this out for anyone who want to know...
require_once("../library/Prince/prince.php");
$princeSettings =$this->getInvokeArg('bootstrap')->getOption('prince');
$prince = new Prince($princeSettings['path']); $prince->setHTML(true);
$pdfPath =realpath(APPLICATION_PATH . "/../public/files/reports/report.pdf");
$prince->convert_string_to_file($this->htmlView, $pdfPath);
I'm writing an application that uses a .php script to get tweets using the twitter search API.
See below code:
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
// Local path
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag. "%20-RT") or die("Could not get tweets");
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
?>
My problem is that I want to make sure that this script runs without fail, or if it does fail at least doesn't keep looping.
The script is going to be run automatically every 1 minute.
Would anyone know a good way to handle errors here?
TL;DR: How do I handle errors on my code?
in simple, use '#' prefix for function. It suppresses errors from displaying. Read More Here
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = #file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag . "%20-RT");
if (!empty($json)) {
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
} else {
echo "Could not get tweets";
exit;
}
?>