Solved

Magneto product importing through API script

Posted on 2016-11-07
10
32 Views
Last Modified: 2016-11-08
$query= "SELECT sku from catalog_product_entity ";
$result = mysqli_query($conn,$query);
$prods = array();


foreach($prods as $prod) {
    $product = $soap->call($session_id, 'catalog_product.info', $prod);
    if($product['type']=="simple"){
        //copy product
        $session_id_2 = $soap_2->login( $mage_2_user, $mage_2_api_key );
        $result = $soap_2->call($session_id_2, 'catalog_product.create', array($product['type'], $product['set'], $product['sku'], $product));
        //copy options
        $product_options = $soap->call($session_id, 'product_custom_option.list', $product['sku']);
        $product_options_data = array();
        foreach($product_options as $product_options_data){
            $product_options_get_data = $soap->call($session_id, 'product_custom_option.info', $product_options_data['option_id']);
            //FIX
            for($i=0;$i < count($product_options_get_data['additional_fields']) ;$i++){
                unset($product_options_get_data['additional_fields'][$i]['value_id']);
            }
            $result2 = $soap_2->call($session_id_2,"product_custom_option.add", array($result, $product_options_get_data));
        }

        //copy media
        $product_images = $soap->call($session_id, 'catalog_product_attribute_media.list', $product['sku']);
        echo "--".count($product_images)."--";
        for($i=0;$i < count($product_images) ;$i++){
            unset($product_images[$i]['file']);
            $curl = curl_init($product_images[$i]['url']);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
            $ret_val = curl_exec($curl);
            if(!curl_errno($curl)){
                $file = array(
                    'content' => chunk_split(base64_encode($ret_val)),
                    'mime' => curl_getinfo($curl , CURLINFO_CONTENT_TYPE),
                );
                $product_images[$i]['file']=$file;
                $result2 = $soap_2->call($session_id_2,"catalog_product_attribute_media.create", array($result, $product_images[$i]));
            } else {
                print_r(curl_error($curl));
            }
            curl_close($curl);
        }
}

Open in new window


The code above displays a page where it asked you to put the SKU of the product and it'll import it to the other store for you, however, I want to get all the products and have it import on its own without asking me for the SKU. I tried this by creating a query and wanting to put them as an array, but it didnt work, can anyone tell my why?
0
Comment
Question by:Jasmine Ikhreishi
  • 6
  • 4
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41877301
Looks like $prods is an empty array.  Did you want to retrieve a results set from the query?
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41878818
I want $prods to have to have all the sku of the products so they can all enter themselves and transfer product manually, i changed the code a bit please check it out (array part)
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41878825
OK so i added this instead of $prods=array();

$query= "SELECT sku from catalog_product_entity ";
$result = mysqli_query($conn,$query);

$prods = array();

while($row=mysqli_fetch_array($result)){
	$prodts= $row['sku'];
	$prods[] = $prodts;
	
}

Open in new window


but when i echo it out, i get nothing.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41878841
This is kind of abbreviated (better examples are shown in this article) but it may help us find out what the query is doing.  PHP var_dump() is your friend.
$query  = "SELECT sku from catalog_product_entity ";
$result = mysqli_query($conn,$query);
if (!$result) trigger_error('Query Failure', E_USER_ERROR);

$prods = array();
while($row = mysqli_fetch_assoc($result)){
    $prods[] = $row['sku'];
}

// SHOW WHAT INFORMATION WAS FOUND
var_dump($prods);

Open in new window

0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41878852
I get the sku now, but it is not enter inside the box where i can copy the productsku.png I want it to be inside that textbox so all the products automatically copy without having to input on sku at a time
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41878895
I found the issue :
    <label for="SKU">SKU</label>
    <input name="SKU" type="text" id="SKU" value="<?php $_POST['sku']; ?>" size="15" maxlength="50" />
  </p>

Open in new window



I tried changing the $_POST['sku'] to $prods but the output was "array" when i used print_r and array(0){} when i used vardump

How could i fix this?
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41878917
Nevermind, my array is null, why is that?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41878925
Is the $prods array null?  Or is it the $_POST array that is null?
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41878967
okay so I fixed that issue, there was some issue with my database connection but I was also wondering, it wouldn't copy my images since it says SSL error. this is the image code part:
for($i=0;$i < count($product_images) ;$i++){
            unset($product_images[$i]['file']);
            $curl = curl_init($product_images[$i]['url']);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
            $ret_val = curl_exec($curl);
            if(!curl_errno($curl)){
                $file = array(
                    'content' => chunk_split(base64_encode($ret_val)),
                    'mime' => curl_getinfo($curl , CURLINFO_CONTENT_TYPE),
                );
                $product_images[$i]['file']=$file;
                $result2 = $soap_2->call($session_id_2,"catalog_product_attribute_media.create", array($result, $product_images[$i]));
            } else {
                print_r(curl_error($curl));
            }
            curl_close($curl);
        }

Open in new window

Is there a way to make curl accept https web as well?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41879029
Yes, cURL can use HTTPS.  There are cURL options.  Here is my teaching example showing how.  
<?php // /demo/curl_get_response_object_example.php
/**
 * Demonstrate the basics of cURL GET-method request
 *
 * http://curl.haxx.se/libcurl/c/libcurl-errors.html
 */
error_reporting(E_ALL);


// USAGE EXAMPLE: BECAUSE IT IS ON MY SERVER, I HAVE HARD-CODED THIS
$url = 'https://twitter.com/RayPaseur';

// TRY THE REMOTE WEB SERVICE
$response = new GET_Response_Object($url);

// SHOW THE WORK PRODUCT
echo "<pre>";
if (!$response->document) var_dump($response);
echo htmlentities($response->document);

// SHOW THE COOKIES, IF ANY
echo PHP_EOL;
echo file_get_contents('cookie.txt');


Class GET_Response_Object
{
    public $href, $title, $http_code, $errno, $info, $document;

    public function __construct($href, $user=NULL, $pass=NULL, $get_array=[], $title=NULL)
    {
        // ACTIVATE THIS TO AVOID TIMEOUT FOR LONG RUNNING SCRIPT
        // set_time_limit(10);

        // STORE THE CALL INFORMATION
        $this->href  = $href;
        $this->title = $title;

        // PREPARE THE GET STRING
        $get_string = http_build_query($get_array);
        if ($get_string) $get_string = '?' . $get_string;

        // MAKE THE REQUEST
        if (!$response = $this->my_curl($href, $user, $pass, $get_string))
        {
            // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
            trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
        }
        else
        {
            return $response;
        }
    }

    protected function my_curl($url, $user, $pass, $get_string, $timeout=3)
    {
        // PREPARE THE CURL CALL
        $curl = curl_init();

        // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
        $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

        // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
        curl_setopt( $curl, CURLOPT_URL,            $url . $get_string  );
        curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:44.0) Gecko/20100101 Firefox/44.0'  );
        curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
        curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
        curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
        curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
        curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );
        curl_setopt( $curl, CURLOPT_HTTPAUTH,       CURLAUTH_ANY );
        curl_setopt( $curl, CURLOPT_USERPWD,        "$user:$pass" );

        curl_setopt( $curl, CURLOPT_VERBOSE,        TRUE   );
        curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE   );

        // SET THE LOCATION OF THE COOKIE JAR (THIS FILE WILL BE OVERWRITTEN)
        curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookie.txt' );
        curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookie.txt' );

        // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
        // http://php.net/manual/en/function.curl-setopt.php#110457
        // http://php.net/manual/en/function.curl-setopt.php#115993
        // http://php.net/manual/en/function.curl-setopt.php#113754
        // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );

        // RUN THE CURL REQUEST AND GET THE RESULTS
        $this->document  = curl_exec($curl);
        $this->errno     = curl_errno($curl);
        $this->info      = curl_getinfo($curl);
        $this->http_code = $this->info['http_code'];
        curl_close($curl);

        return $this;
    }
}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now