Export Import Blogs Shopify API

This tutorial explains how to export import blogs shopify API. Using Shopify API data can be export from one store and import to another programmatically.

Please refer other PHP tutorials

Step by step explanation

  • Shopify API Connection
  • Export Shopify Blogs
  • Import Shopify Blogs

Shopify API Connection

Establish shopify API connection and build the base url.

Export_Import_Blogs_Shopify_API

Export Shopify Blogs

The following code export the blogs from the store.

 /**
 * Get base url and access the blog data
 * @return type
 */

public function getBlogs() {
    
    $api = 'blogs.json';
    $baseUrl = $this->objauth->getApiUrl();
    $blogs = $this->objaccess->getContent($baseUrl.$api);
    return $blogs;
    
}

/**
 * Get Data using api
 * @param type $url
 * @return type
 */
public function getContent( $url ) {
 
    $response = file_get_contents( $url );
    $data = json_decode( $response, true );
    return $data;
    
}

Import Shopify Blogs

This step create the blog data array as shown below.

export_import_blogs_shopify_API_PHP

public function postBlogs() {
    
    $api = 'blogs/2234777628/articles.json';
    $baseUrl = $this->objauth->getApiUrl().$api;
    $blogs = $this->blogData();
    $response = $this->objaccess->postContents( $blogs, $baseUrl );
    return $response;
    
}

public function blogData() {
    
    $blogDataArr = array(
        'article' =>array( 
            'title' => 'Blog Created',
            'body_html' =>'Test Blog Content',
            'tags' => 'Blog Created',
            'published_at' => date('Y-m-d H:i:s'),
            'author' => 'barbflies Admin'
        )
    );
    
    return $blogDataArr;   
}

Post Shopify API

public function postContents( $dataArray, $postUrl ) {
        
    try{

        $requestJson = json_encode( $dataArray );
        $postlength = strlen($requestJson);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$postUrl);
        curl_setopt($ch,CURLOPT_POST,$postlength);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;

    } catch (Exception $ex) {
            echo 'Post data error: ' . $ex->getMessage();
    } 
}

create-blogs-shopify-api
Export Import blogs Shopify api full code.

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*