我如何从本地系统网站设置创建应用程序并将文件部署到shopify合作伙伴帐户



我想从本地系统网站设置中创建一个应用程序。但我遵循的程序不正常。我看了一些视频教程,但它没有提供所需的输出。我创建了四个名为generate_token.php、install.php、api_call_write_products.php、inc文件夹的文件,其中包括一个functions.php文件。我从合作伙伴帐户的管理站点创建了一个私人应用程序,但它仍然不起作用。我如何将这些文件上传到我的合作伙伴帐户中。是否有任何第三方工具可以上传/部署包括inc文件夹在内的四个文件?如何将我的本地文件集成到合作伙伴帐户中,以便在合作伙伴帐户上查看我的应用程序。我将API密钥和Sharedsecret密钥添加到需要它们的文件中。我添加了以下文件-

install.php

<?php
// Set variables for our request
$shop = $_GET['shop'];
$api_key = "***************";
$scopes = "read_orders,write_products";
$redirect_uri = "https://dd351ab1814cec97dcb5e8d919887c2a:abb65451f1f5c828011242c806971e2b@bahlultechstore.myshopify.com/admin/api/2020-01/orders.json/generate_token.php";
// Build install/approval URL to redirect to
$install_url = "https://" . $shop . ".myshopify.com/admin/oauth/authorize?client_id=" . $api_key . "&scope=" . $scopes . "&redirect_uri=" . urlencode($redirect_uri);
// Redirect
header("Location: " . $install_url);
die();

generate_token.php

<?php
// Get our helper functions
require_once("inc/functions.php");
// Set variables for our request
$api_key = "**********************";
$shared_secret = "***********************";
$params = $_GET; // Retrieve all request parameters
$hmac = $_GET['hmac']; // Retrieve HMAC request parameter
$params = array_diff_key($params, array('hmac' => '')); // Remove hmac from params
ksort($params); // Sort params lexographically
$computed_hmac = hash_hmac('sha256', http_build_query($params), $shared_secret);
// Use hmac data to check that the response is from Shopify or not
if (hash_equals($hmac, $computed_hmac)) {
// Set variables for our request
$query = array(
"client_id" => $api_key, // Your API key
"client_secret" => $shared_secret, // Your app credentials (secret key)
"code" => $params['code'] // Grab the access key from the URL
);
// Generate access token URL
$access_token_url = "https://" . $params['shop'] . "/admin/oauth/access_token";
// Configure curl client and execute request
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POST, count($query));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
$result = curl_exec($ch);
curl_close($ch);
// Store the access token
$result = json_decode($result, true);
$access_token = $result['access_token'];
// Show the access token (don't do this in production!)
echo $access_token;
} else {
// Someone is trying to be shady!
die('This request is NOT from Shopify!');
}

api_call_write_products.php

// Get our helper functions
require_once("inc/functions.php");
// Set variables for our request
$shop = "demo-shop";
$token = "*******************";
$query = array(
"Content-type" => "application/json" // Tell Shopify that we're expecting a response in JSON format
);
// Run API call to get products
$products = shopify_call($token, $shop, "/admin/products.json", array(), 'GET');
// Convert product JSON information into an array
$products = json_decode($products['response'], TRUE);
// Get the ID of the first product
$product_id = $products['products'][0]['id'];
// Modify product data
$modify_data = array(
"product" => array(
"id" => $product_id,
"title" => "My New Title"
)
);
// Run API call to modify the product
$modified_product = shopify_call($token, $shop, "/admin/products/" . $product_id . ".json", $modify_data, 'PUT');
// Storage response
$modified_product_response = $modified_product['response'];

函数.php

<?php
function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', 
$request_headers = array()) {
// Build URL
$url = "https://" . $shop . ".myshopify.com" . $api_endpoint;
if (!is_null($query) && in_array($method, array('GET',  'DELETE'))) $url = $url . "?" . http_build_query($query);
// Configure cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
// Setup headers
$request_headers[] = "";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
if (is_array($query)) $query = http_build_query($query);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
}
// Send request to Shopify and capture any errors
$response = curl_exec($curl);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
// Close cURL to be nice
curl_close($curl);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/rnrn|nn|rr/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
return array('headers' => $headers, 'response' => $response[1]);
}
}

您不将文件上传到Shopify,而是将它们托管在自己的服务器上。

如果您想在本地测试代码,请使用ngrok。你可以在这里找到如何做到这一点的步骤。

当你想要一个prod环境时,看看heroku来托管你的代码。

最新更新