Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nielsenrc/9b26016d9038421c1ff7 to your computer and use it in GitHub Desktop.
Save nielsenrc/9b26016d9038421c1ff7 to your computer and use it in GitHub Desktop.
Shopify | PHP | Use Shopify API to Bulk Import 301 Redirects
<?php
// In Shopify go to Apps -> Private Apps to get API Key
// List of Redirects under Online Store -> Navigation -> Redirects
$api_key = "";
$password = "";
$store_subdomain = ""; //
$shopify_url = "https://" . $api_key . ":" . $password . "@" . $store_subdomain . ".myshopify.com/admin/redirects.json";
$array_of_redirects = array (
'[/from-uri]' => '[/to-uri]',
);
foreach($array_of_redirects as $key => $value) {
$array = array (
'redirect' => array (
'path' => $key,
'target' => $value
),
);
$json_object = json_encode($array, JSON_PRETTY_PRINT);
print_r($json_object);
$header = array();
$header[] = "Content-Type:application/json";
$options = array(
CURLOPT_URL => $shopify_url,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $json_object
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close ($ch);
//to avoid getting throttled by shopify
sleep(1);
var_dump($response);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment