Quantcast
Channel: Opinion: Shopify Community - Shopify Apps
Viewing all articles
Browse latest Browse all 11091

Creating webhook in app by boski mehta

$
0
0

I am working on creating app and needs the webhook  to get the notification on new oder creation, user account creation etc.I have used the following code
<?php
    if(!session_id()) session_start();
    require __DIR__.'/vendor/autoload.php'

;
    use phpish\shopify;
    require __DIR__.'/conf.php';
    isset($_REQUEST['shop']) or die ('Query parameter "shop" missing.');
    preg_match('/^[a-zA-Z0-9\-]+.myshopify.com$/', $_REQUEST['shop']) or die('Invalid myshopify.com store URL.');
    $oauth_token = shopify\access_token($_REQUEST['shop'], SHOPIFY_APP_API_KEY, SHOPIFY_APP_SHARED_SECRET, $_REQUEST['code']);
    $_SESSION['oauth_token'] = $oauth_token;
    $_SESSION['shop'] = $_REQUEST['shop'];
   
    $data = array(
            'access_token' => $oauth_token,
            'webhook' => array(
                'address' => 'https://smscountry.herokuapp.com/notify.php',
                'format' => 'json',
                'topic' => 'orders/create',
            )
        );
    $data_string = json_encode($data);                                                                         
    $ch = curl_init("https://smsappstore.myshopify.com/admin/webhooks.json");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                         
        'Content-Type: application/json',                                                                               
        'Content-Length: ' . strlen($data_string))                                                                      
    );
    $response = curl_exec($ch);
    echo "<pre>";
    print_r(json_decode($response));
    die;
    echo "<script>window.location = 'https://smsappstore.myshopify.com/admin/apps';</script>";
    exit();
?>

But when i checked the store setting no webhook is created.
Please assist me to fix this issue ASAP.

Thanks


Viewing all articles
Browse latest Browse all 11091

Trending Articles