不成功地从Android应用程序添加产品到woocommerce

I want add new product to woocommerce from android app. So I wrote below code :

    <?php

    require_once("wp-load.php");

    require_once("wp-load.php");

    $image = $_POST['image'];
    $name = $_POST['name'];
    $info = $_POST['info'];
    $price = $_POST['price'];
    $saleprice = $_POST['saleprice'];
    $id = $_POST['id'];
    $imageName = $_POST['imageName'];
    $catid = $_POST['catID'];






    //add new product 
    $args = array(     
        'post_author' =>$id, 
        'post_content' => $info,
        'post_status' => "Publish", // (Draft | Pending | Publish)
        'post_title' =>$name,
        'post_parent' => '',
        'post_type' => "product"
    );
    // Create a simple WooCommerce product
    $postId = wp_insert_post( $args );

    //add category to new product
    $cat=array($catid);
    wp_set_object_terms($postId,$cat, 'product_cat');

    // Setting the product type
    wp_set_object_terms( $postId, 'simple', 'product_type' );

    // Setting the product price

    update_post_meta( $postId, '_price',$price );
    if($saleprice!=null)
        update_post_meta( $postId, '_sale_price', $saleprice );


    // current path directory
    $dirPath = getcwd();

    // full path of image
    $IMGFilePath = $dirPath.'/'.$imageName;


    $logtxt .= $IMGFilePath. "   IMGFilePathl
";
    // error message for file not found in directory
    $message = $imageName.' is not available or found in directory.';




    //prepare upload image to WordPress Media Library
    $upload = wp_upload_bits( $imageName, null, base64_decode($image) );



    // check and return file type
    $imageFile = $upload['file'];
    $wpFileType = wp_check_filetype($imageFile, null);

    // Attachment attributes for file
    $attachment = array(
        'post_mime_type' => $wpFileType['type'],  // file type
        'post_title' => sanitize_file_name($imageFile),  // sanitize and use image name as file name
        'post_content' => '',  // could use the image description here as the content
        'post_status' => 'inherit'
     );

    // insert and return attachment id
    $attachmentId = wp_insert_attachment( $attachment, $imageFile, $postId );


    // insert and return attachment metadata
    $attachmentData = wp_generate_attachment_metadata( $attachmentId, $imageFile);

    // update and return attachment metadata
    wp_update_attachment_metadata( $attachmentId, $attachmentData );


    // finally, associate attachment id to post id
    $success = set_post_thumbnail( $postId, $attachmentId );


    echo $postId;

    ?>

I encoded image in android and send it to php file and decoded it and uploaded it in wordpress .After that I creat new peroduct .And then I Attach image to the new post. The problem is that my product is added twice and the category and image are not added correctly. How can I fix it ?

just changed it to below code .so it worked successfully.

<?php

    require_once("wp-load.php");

    require_once("wp-load.php");

    $image = $_POST['image'];
    $name = $_POST['name'];
    $info = $_POST['info'];
    $price = $_POST['price'];
    $saleprice = $_POST['saleprice'];
    $id = $_POST['id'];
    $imageName = $_POST['imageName'];
    $catid = $_POST['catID'];






    //add new product 
    $args = array(     
        'post_author' =>$id, 
        'post_content' => $info,
        'post_status' => "Publish", // (Draft | Pending | Publish)
        'post_title' =>$name,
        'post_parent' => '',
        'post_type' => "product"
    );
    // Create a simple WooCommerce product
    $postId = wp_insert_post( $args );

    //add category to new product
    $cat=array($catid);
    wp_set_object_terms($postId,$cat, 'product_cat');

    // Setting the product type
    wp_set_object_terms( $postId, 'simple', 'product_type' );

    // Setting the product price

    update_post_meta( $postId, '_price',$price );
    if($saleprice!=null)
        update_post_meta( $postId, '_sale_price', $saleprice );


    $upload = wp_upload_bits( $imageName, null, base64_decode($image) );    

    // check and return file type
    $image_url = $upload['url'];





    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename =$imageName;
    if(wp_mkdir_p($upload_dir['path']))     $file = $upload_dir['path'] . '/' . $filename;
    else                                    $file = $upload_dir['basedir'] . '/' . $filename;
    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($filename),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $file, $postId );
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
    $res2= set_post_thumbnail( $postId, $attach_id );
    var_dump($res2);
    ?>