I want to use shopify order webhook to store order data into mysql database. I am beginner in webhooks. Googled many times to find the guidance about how to achieve it.I found this solutions is very close for me.I wrote the php code but I am getting error like this error img.I don't know why I am getting this error and how to resolve it.Please suggest us.
I followed these steps
I created the webhook from shopify admin dashboard
Point the webhook to my own domain
And write the following code in the pointed link
here is my code
my code is
<?php
$webhook_content = NULL;
// Get webhook content from the POST
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhook_content .= fread($webhook, 4096);
}
fclose($webhook);
// Decode Shopify POST
$webhook_content = json_decode($webhook_content, TRUE);
$servername = "localhost";
$database = "xxxxxxxx_xxxhalis";
$username = "xxxxxxxx_xxxhalis";
$password = "***********";
$sql = "mysql:host=$servername;dbname=$database;";
// Create a new connection to the MySQL database using PDO, $my_Db_Connection is an object
try {
$db = new PDO($sql, $username, $password);
//echo "<p> DB Connect = Success.</p>";
} catch (PDOException $error) {
echo 'Connection error: ' . $error->getMessage();
}
$order_num= $webhook_content['name'];
$order_date = $webhook_content['created_at'];
$order_mode = "Online";
$location= $webhook_content['default_address']['province'];
$cust_name = $webhook_content['billing_address']['name'];
$address = $webhook_content['default_address']['address1']['address2']['city']['province']['zip'];
$phone_num = $webhook_content['default_address']['phone'];
$special_note= $webhook_content['note'];
$total_mrp = $webhook_content['current_subtotal_price'];
$total_discount= $webhook_content['current_total_discounts'];
$sub_total = $webhook_content['current_subtotal_price'];
$delivery_charges = $webhook_content['presentment_money']['amount'];
$totalOrderValues= $webhook_content['total_price'];
$discount_approval = "NA";
$invoice_status= "NA";
$punching_status = "NA";
$order_source = "Shopify";
$payment_mode= $webhook_content['payment_gateway_names'];
$payement_status = "Done";
$payement_recieve_date= $webhook_content['processed_at'];
$reference_number = $webhook_content['reference'];
$cash_handover_status = "NA";
$my_Insert_Statement = $my_Db_Connection->prepare("INSERT INTO `customer_order_sab`( `order_num`, `order_datetime`, `modeOfOrder`, `location`, `customer_name`, `address`, `phone_number`, `special_note`, `total_mrp`, `total_discount`, `sub_total`, `delivery_charges`, `totalOrderValues`, `discount_approval`, `invoice_status`, `punching_status`, `order_source`, `payment_mode`, `payement_status`, `payement_recieve_date`, `reference_number`, `cash_handover_status`) VALUES (:order_num, :order_date, :order_mode, :location, :cust_name, :address, :phone_num, :special_note, :total_mrp, :total_discount, :sub_total, :delivery_charges, :totalOrderValues, :discount_approval, :invoice_status, :punching_status, :order_source, :payment_mode, :payement_status, :payement_recieve_date, :reference_number, :cash_handover_status)");
$my_Insert_Statement->bindParam(':order_num', $order_num);
$my_Insert_Statement->bindParam(':order_date', $order_date);
$my_Insert_Statement->bindParam(':order_mode', $order_mode);
$my_Insert_Statement->bindParam(':location', $location);
$my_Insert_Statement->bindParam(':cust_name', $cust_name);
$my_Insert_Statement->bindParam(':address', $address);
$my_Insert_Statement->bindParam(':phone_num', $phone_num);
$my_Insert_Statement->bindParam(':special_note', $special_note);
$my_Insert_Statement->bindParam(':total_mrp', $total_mrp);
$my_Insert_Statement->bindParam(':total_discount', $total_discount);
$my_Insert_Statement->bindParam(':sub_total', $sub_total);
$my_Insert_Statement->bindParam(':delivery_charges', $delivery_charges);
$my_Insert_Statement->bindParam(':totalOrderValues', $totalOrderValues);
$my_Insert_Statement->bindParam(':discount_approval', $discount_approval);
$my_Insert_Statement->bindParam(':invoice_status', $invoice_status);
$my_Insert_Statement->bindParam(':punching_status', $punching_status);
$my_Insert_Statement->bindParam(':order_source', $order_source);
$my_Insert_Statement->bindParam(':payment_mode', $payment_mode);
$my_Insert_Statement->bindParam(':payement_status', $payement_status);
$my_Insert_Statement->bindParam(':payement_recieve_date', $payement_recieve_date);
$my_Insert_Statement->bindParam(':reference_number', $reference_number);
$my_Insert_Statement->bindParam(':cash_handover_status', $cash_handover_status);
if ($my_Insert_Statement->execute()) {
echo "New record created successfully";
} else {
echo "Unable to create record";
}
?>
You are doing it wrong in one giant respect, but I respect you for trying!
If you expect webhooks to be sent to your App from some store where you installed your App, and you expect to save data from the webhooks in your database you cannot create the webhooks from the Shopify Admin and then process them in your App due to security. Instead, you install a webhook from your App, instructing Shopify to send your App webhooks. This way, Shopify can use your App API token to secure the webhooks. Your App then receives webhooks and decides if they are legit or not. You cannot do that if you use the Admin.
Read up on creating webhooks for use with your App! You can choose RestAPI or GraphQL and go from there.
guys currently I build a web site using PHP. I connect my website to the database using web services (url). The system is run as well. Then, I separate the web services by creating other PHP files, place the web services and include the PHP file to my web site. This is also running well. Below is the code:
add_factory.php
<?php
include("../../config/check.php");
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
include("../../api/api_add_factory.php"); //include web services 1
if(empty($queryt)){
include("../../api/api2_add_factory.php"); //include web services 2
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
?>
api_add_factory.php
<?php
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$data = file_get_contents($url);
$json = json_decode($data);
$queryt = $json->factoryList;
?>
api2_add_factory.php
<?php
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
$data2 = file_get_contents($url2);
$json2 = json_decode($data2);
?>
When you see at "add_factory.php", There's two include. Now, I want to use only single include that can call "api_add_factory.php" and "api2_add_factory.php". But I dont know how to do that since the "include" at "add_factory.php" is at different position.
Can anyone knows how to merge both include file into one and where i need to pu the include file at "add_factory.php"?
My suggestion would be to rewrite the contents of api_add_factory.php and api2_add_factory.php as functions.
This would allow you to control how and when they are called by the piece of code that includes them, rather than being executed immediately when the include is called.
Also, as a general rule of thumb, includes should all be called at the beginning of your file because otherwise they can become very difficult to keep track of.
So, I would say you can do something like this:
api_add_factory.php
<?php
function callApi1()
{
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$data = file_get_contents($url);
$json = json_decode($data);
return $json->factoryList;
}
api2_add_factory.php
<?php
function callApi2()
{
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
$data2 = file_get_contents($url2);
return json_decode($data2);
}
add_factory.php
<?php
include("../../config/check.php");
include("../../api/api_add_factory.php"); //include web services 2
include("../../api/api2_add_factory.php"); //include web services 2
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
$queryt = callApi1();
if(empty($queryt)){
$json2 = callApi2();
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
Actually, now that I am taking a look at my answer, I can see a possibility for a further improvement: since the contents of api_add_factory.php and api2_add_factory.php are actually the same apart from the url that gets called, you can do something like this:
api_add_factory.php
<?php
function callApi($url)
{
$data = file_get_contents($url);
return json_decode($data);
}
add_factory.php
<?php
include("../../config/check.php");
include("../../api/api_add_factory.php"); //include web services
$url1 = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID";
$url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
if(isset($_POST['Submit']))
{
$Fac_ID = $_POST['Fac_ID'];
$json1 = callApi($url1);
$queryt = $json1->factoryList;
if(empty($queryt)){
$json2 = callApi($url2);
if(!empty($json2)){
header("Location:factory.php");
}else{
echo "
<script>alert('Something wrong, please try again')</script>
<script>window.location = 'factory.php'</script>
";
}
}
else{
echo "
<script>alert('The factory you want to add is already exist')</script>
<script>window.location = 'factory.php'</script>
";
}
}
This way, we don't need to repeat the code of the function.
The code I am using is:
$url = "example.com";
$code = file_get_contents("http://www.".$url);
if (!$code) {
$code = file_get_contents("https://www.".$url);
}
I don't know whether each URL starts with http or https. I have 1,000 URLs saved in my database. Any suggestions?
This answer is somewhat relevant. Given that your database is rather incomplete in that the URLs aren't fully qualified, I would be inclined to write a short routine to update the database for future use.
<?php
$url = 'example.com';
$variations[] = 'http://'.$url;
$variations[] = 'https://'.$url;
$variations[] = 'http://www.'.$url;
$variations[] = 'https://www'.$url;
foreach($variations as $v){
// Check variation with function from answer linked above
if(urlOK($v)){
// code to update database row
// or if you don't want to do that
$code = file_get_contents($v);
}
}
I have big problem because, when I'm trying to show status of my ts3 server I have blank page... What am I doing wrong?
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
try
{
// connect to server, authenticate and grab info
$ts3 = TeamSpeak3::factory("serverquery://query_admin:query_pass#host:10011/?server_port=9987");
// show server as online
$serverinfo[$j]['hostname'] = $ts3->virtualserver_name;
$serverinfo[$j]['online'] = 'online';
$serverinfo[$j]['players'] = $ts3->virtualserver_clientsonline;
$serverinfo[$j]['max'] = $ts3->virtualserver_maxclients;
}
catch(Exception $e)
{
// grab errors and show server as offline
$serverinfo[$j]['online'] = 'offline';
$serverinfo[$j]['players'] = '-';
$serverinfo[$j]['max'] = '-';
}
When I comment this code the page shows as normal...
EDIT:
I see it now, if I only add this
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
and nothing more to my code it shows blank page... Is it possible, that library from here doesn't work properly?
You forgot to add echo before each info so the code becomes :
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
try
{
// connect to server, authenticate and grab info
$ts3 = TeamSpeak3::factory("serverquery://query_admin:query_pass#host:10011/?server_port=9987");
// show server as online
echo $serverinfo[$j]['hostname'] = $ts3->virtualserver_name;
echo $serverinfo[$j]['online'] = 'online';
echo $serverinfo[$j]['players'] = $ts3->virtualserver_clientsonline;
echo $serverinfo[$j]['max'] = $ts3->virtualserver_maxclients;
}
catch(Exception $e)
{
// grab errors and show server as offline
echo $serverinfo[$j]['online'] = 'offline';
echo $serverinfo[$j]['players'] = '-';
echo $serverinfo[$j]['max'] = '-';
}
I can access this script directly via URL and it works fine but as a cron job ot doesnt work. Is it aweber or am I doing something wrong?
Awebre's documentation is one of the worst ones I have ever come across!
I am not sure why there is no explanation of this in their docs!
Thanks
<?php
include "wp-load.php";
include_once('wp-includes/class-phpass.php');
$sql = "SELECT member_id, email FROM wp_members_tbl WHERE aweber != 1";
$result = $wpdb->get_results($sql);
if(count($result)>0)
{
##Add aweber
require_once('aweber/aweber_api/aweber_api.php');
$consumerKey = '***';
$consumerSecret = '***';
$accessKey = '***'; # put your credentials here
$accessSecret = '***'; # put your credentials here
$account_id = '***'; # put the Account ID here
$list_id = '***'; # put the List ID here 3823593
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
# Get an access token
if(empty($_COOKIE['accessToken']))
{
if (empty($_GET['oauth_token']))
{
$callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
list($requestToken, $requestTokenSecret) = $aweber->getRequestToken($callbackUrl);
setcookie('requestTokenSecret', $requestTokenSecret);
setcookie('callbackUrl', $callbackUrl);
header("Location: {$aweber->getAuthorizeUrl()}");
exit();
}
$aweber->user->tokenSecret = $_COOKIE['requestTokenSecret'];
$aweber->user->requestToken = $_GET['oauth_token'];
$aweber->user->verifier = $_GET['oauth_verifier'];
list($accessToken, $accessTokenSecret) = $aweber->getAccessToken();
setcookie('accessToken', $accessToken);
setcookie('accessTokenSecret', $accessTokenSecret);
header('Location: '.$_COOKIE['callbackUrl']);
exit();
}
##End add aweber
foreach($result as $val=>$row)
{
# Get AWeber Account
try {
$account = $aweber->getAccount($_COOKIE['accessToken'], $_COOKIE['accessTokenSecret']);
$listURL = "https://api.aweber.com/1.0/accounts/***/lists/".$list_id;
$list = $account->loadFromUrl($listURL);
$params = array(
'email' => $row->email
);
$subscribers = $list->subscribers;
$new_subscriber = $subscribers->create($params);
$update_data = array('aweber' => 1);
$where = array('member_id' => $row->member_id);
$wpdb->update( 'wp_members_tbl', $update_data, $where, $format = null, $where_format = null);
# success!
//print "A new subscriber was added to the $list->name list!";
}
catch(AWeberAPIException $exc)
{
print "<h3>AWeberAPIException:</h3>";
print " <li> Type: $exc->type <br>";
print " <li> Msg : $exc->message <br>";
print " <li> Docs: $exc->documentation_url <br>";
print "<hr>";
//exit(1);
}
}
}
Looks like a few places where this could be failing:
1) You have relative paths in your includes. The cron may not be able to find the included files. Try changing these to full paths.
2) Your code requires a browser environment (setting cookies, http header Location etc). None of this will work outside of a browser.
You should always execute the file from the command line as the user the cron runs as to test it. Any errors will be obvious.
To fix your issue you should use this tutorial which uses the browser to setup the credentials. After that it can be run from the command line/cron:
http://engineering.aweber.com/quick-start-api-script-in-php/
Try using this in the cron command:
wget -qO- http://yoururlhere/ &> /dev/null
It's not the best solution since "self calls" aren't recommended, but it'll keep you from dealing with environment settings.