[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

Magneto product importing through API script

$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
Jazzy 1012
Asked:
Jazzy 1012
  • 6
  • 4
1 Solution
 
Ray PaseurCommented:
Looks like $prods is an empty array.  Did you want to retrieve a results set from the query?
0
 
Jazzy 1012Author Commented:
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
 
Jazzy 1012Author Commented:
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
Ray PaseurCommented:
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
 
Jazzy 1012Author Commented:
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
 
Jazzy 1012Author Commented:
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
 
Jazzy 1012Author Commented:
Nevermind, my array is null, why is that?
0
 
Ray PaseurCommented:
Is the $prods array null?  Or is it the $_POST array that is null?
0
 
Jazzy 1012Author Commented:
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
 
Ray PaseurCommented:
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now