Solved

Magneto product importing through API script

Posted on 2016-11-07
10
62 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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

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
 
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 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

809 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