Last active
April 27, 2018 20:50
-
-
Save AKSarav/9fc6d095c6440c9f6f34110f12525ab9 to your computer and use it in GitHub Desktop.
Webhook program to receive data from some remote client and process it and send to Airtable and add a record in table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Processor { | |
public function execute($targetFile) { | |
$data = sprintf( | |
"%s %s %s \n\nHTTP headers:\n", | |
$_SERVER['REQUEST_METHOD'], | |
$_SERVER['REQUEST_URI'], | |
$_SERVER['SERVER_PROTOCOL'] | |
); | |
foreach ($this->getHeaderList() as $name => $value) { | |
$data .= $name . ': ' . $value . "\n"; | |
} | |
$data .= "\nRequest body:\n"; | |
file_put_contents( | |
$targetFile, | |
$data . urldecode(file_get_contents('php://input')) . "\n" | |
); | |
ob_start(); | |
$request_headers = array(); | |
$request_headers[]= 'Accept: application/json'; | |
$request_headers[]= 'Authorization: Bearer mykey'; | |
$request_headers[]= 'Content-Type: application/json'; | |
$post_data=[]; | |
#Process the Array | |
foreach ( $_POST as $key => $val ) | |
{ | |
if ($key == 'Caller_Number') | |
{ | |
$val=str_replace('"', '', $val); | |
print "\nFound Caller Number" . $val; | |
$post_data['fields']=array('Mobile Number' => $val); | |
} | |
} | |
print "\n\nPOST DATA PROCESSING\n----------------------------------\n"; | |
echo str_replace("Array","",$post_data); | |
$curl_connection = curl_init('https://api.airtable.com/v0/appOwnQngw2elSeVN/sometablef'); | |
if (!$curl_connection) { | |
echo "\nError in opening CURL Connection\n"; | |
} | |
//set headers | |
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $request_headers); | |
//set options | |
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); | |
#curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); | |
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($curl_connection, CURLOPT_VERBOSE, 1); | |
curl_setopt($curl_connection, CURLINFO_HEADER_OUT, true); | |
//Remove the word Array from the json encode output | |
$tosend = str_replace("Array","",json_encode($post_data)); | |
//Remove the Quotes around the number when sending the output | |
$pattern = '/\"([0-9]+)\"/i'; | |
$replacement = '${1}'; | |
$tosend = preg_replace($pattern, $replacement, $tosend); | |
print "\n\nNUMBER BEING SENT".$tosend; | |
//Add the data into POSTFIELDS | |
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $tosend); | |
//perform our request | |
$result = curl_exec($curl_connection); | |
//show information regarding the request | |
print "\n\n\nCURL REQUEST LOG \n----------------------------------\n"; | |
print_r(curl_getinfo($curl_connection)); | |
echo curl_errno($curl_connection) . '-' . | |
curl_error($curl_connection); | |
print "RESULT\n".$result; | |
//close the connection | |
curl_close($curl_connection); | |
$content = ob_get_clean(); | |
file_put_contents($targetFile, $content, FILE_APPEND); | |
echo("God bless you"); | |
} | |
private function getHeaderList() { | |
$headerList = []; | |
foreach ($_SERVER as $name => $value) { | |
if (preg_match('/^HTTP_/',$name)) { | |
// convert HTTP_HEADER_NAME to Header-Name | |
$name = strtr(substr($name,5),'_',' '); | |
$name = ucwords(strtolower($name)); | |
$name = strtr($name,' ','-'); | |
// add to list | |
$headerList[$name] = $value; | |
} | |
} | |
return $headerList; | |
} | |
} | |
(new Processor)->execute('./toairtable.txt'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment