在给出相对路径时,不使用rss Feed在etsy商店上传图像

时间:2018-01-04 15:17:05

标签: wordpress rss etsy

我正在尝试为我拥有的产品生成Feed并在etsy商店分享。我已经做了以下事情。执行此任务的功能是:

提交产品清单:

    function submit_listing_to_etsy()
    {
        global $wpdb;
        $table = $wpdb->prefix . "etcpf_listings";
        $id = false;
        if ($this->check_unsubmitted_listing()) {
            $id = $this->listing['id'];

            $data = maybe_unserialize($this->listing['data']);
            $listing_id = '';
            $type = false;
            if (!(NULL == $this->listing['listing_id'])) {
                $listing_id = '/'.$this->listing['listing_id']; 
                $type = true;
            } 
            unset($data['image']);

            if (isset($data['sku'])) {
                unset($data['sku']); // it is not supported yet
            }
            unset($data['image']);
            if (count($data['tags']) == 1) { // check if tags with 0 value is set
                unset($data['tags']);
            }
            $weight = explode(' ', $data['item_weight']);
            if (isset($weight[1])) {
                $data['item_weight'] = $weight[0];
                $data['item_weight_units'] = $weight[1];
            }

            $length = explode(' ',$data['item_length']);
            if (isset($length[1])) {
                $data['item_length'] = $length[0];
                $data['item_length_units'] = $length[1];
            }

            $width = explode(' ',$data['item_width']);
            if (isset($width[1])) {
                $data['item_width'] = $width[0];
                $data['item_width_units'] = $width[1];
            }

            $height = explode(' ',$data['item_height']);
            if (isset($height[1])) {
                $data['item_height'] = $height[0];
                $data['item_height_units'] = $height[1];
            }

            $data['language'] = substr(get_locale(), 0,2);
            $url = "https://openapi.etsy.com/v2/listings".$listing_id."?scopes=listings_w";
            $acc_req = $this->prepareHash($url,'POST',$type);
            // $response = $this->browsePost($acc_req,json_encode($data),true);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $acc_req);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
            $response = curl_exec($ch);
            curl_close($ch);
            $result = json_decode($response);
            $result = $result->results;    
            $listing = $result[0];
            $this->listing_id = $listing->listing_id;

            if ($this->listing_id) {
                $data = [
                    'listing_id' => $this->listing_id,
                    'upload_result'   => maybe_serialize($listing),
                    'uploaded'      => (strlen($listing_id) > 0) ? 2 : 1
                ];

            } else 
                $data = [
                    'upload_result'     => maybe_serialize($response),
                    'uploaded'          => 3
                ];

            $wpdb->update($table,$data,['id'=>$id]); 
        }
    }

提交这些产品的图片:

     function submit_listing_images(){
        $data = maybe_unserialize($this->listing['data']);
        $image = $data['image'];

        global $wpdb;
        $tbl = $wpdb->prefix . "etcpf_listings";

        $this->check_unsubmitted_listing(1);

        $url =  "https://openapi.etsy.com/v2/listings/".$this->listing['listing_id']."/images";

        $wp_dir = wp_upload_dir();
        $i = str_replace($wp_dir['url'],$wp_dir['path'],$image);


        if ($i == $image)
            $i = str_replace($wp_dir['baseurl'], $wp_dir['basedir'], $image);
        $img_type = wp_check_filetype($image);
        $url_to_send = $this->prepareHash($url,'POST');
        $data = [
            'image'    => '@'.$i.';type='.$img_type['type']
        ];
        if (!$i) {
            $wpdb->update($tbl,['uploaded' => 2],['id'=>$this->listing['id']]);   
            return;
        }
        //print_r($data);exit;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_to_send);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, FALSE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: multipart/form-data"]);
        $response = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($response);
        $result = $result->results;
        $img_data = $result[0];
        $listing_image_id = $img_data->listing_image_id;

        if ($listing_image_id) {
            $wpdb->update($tbl,['listing_image_id'=>$listing_image_id,'uploaded' => 2],['id'=>$this->listing['id']]);   
        }
    }

生成的Feed如下:

    <rss version="2.0">
    <channel>
    <title>test</title>
    <link>...</link>
    <item>
    <id>41</id>
    <title>
    <![CDATA[ Ship Your Idea ]]>
    </title>
    <item_group_id>40</item_group_id>
    <quantity>10</quantity>
    <description>
    <![CDATA[
    Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
    ]]>
    </description>
    <price>35.00</price>
    <image_link>
    http://localhost/wordpress/wp-content/uploads/2013/06/hoodie_7_front.jpg
    </image_link>
    <etsy_category>
    <![CDATA[ Baby Carriers & Wraps:69150455 ]]>
    </etsy_category>
    <weight_unit>kg</weight_unit>
    <dimension_unit>cm</dimension_unit>
    <tags/>
    <currency>NPR</currency>
    </item>
    <item>
    <id>42</id>
    <title>
    <![CDATA[ Ship Your Idea ]]>
    </title>
    <item_group_id>40</item_group_id>
    <quantity>10</quantity>
    <description>
    <![CDATA[
    Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
    ]]>
    </description>
    <price>35.00</price>
    <image_link>
    http://localhost/wordpress/wp-content/uploads/2013/06/hoodie_1_front.jpg
    </image_link>
    <sale_price>30.00</sale_price>
    <etsy_category>
    <![CDATA[ Baby Carriers & Wraps:69150455 ]]>
    </etsy_category>
    <weight_unit>kg</weight_unit>
    <dimension_unit>cm</dimension_unit>
    <tags/>
    <currency>NPR</currency>
    </item>
    <item>
    <id>47</id>
    <title>
    <![CDATA[ Woo Ninja ]]>
    </title>
    <quantity>10</quantity>
    <description>
    <![CDATA[
    Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
    ]]>
    </description>
    <price>35.00</price>
    <image_link>
    http://localhost/wordpress/wp-content/uploads/2013/06/hoodie_2_front.jpg
    </image_link>
    <etsy_category>
    <![CDATA[ Baby Carriers & Wraps:69150455 ]]>
    </etsy_category>
    <weight_unit>kg</weight_unit>
    <dimension_unit>cm</dimension_unit>
    <tags/>
    <currency>NPR</currency>
    </item>
    </channel>
    </rss>

此生成的Feed中的路径是可以的。当我复制并粘贴图像路径时,它会加载图像。但是对于etsy api,相对路径被发送。当我打印包含图像路径的数据时,它会返回如下所示的数组:

    Array ( [image] => @/var/www/html/wordpress/wp-content/uploads/2013/06/hoodie_1_front.jpg;type=image/jpeg )

当我在相应的文件夹中检查它时,特定的图像存在。所以任何人都可以帮助我解决这里真正的问题吗?有趣的是,有时从一些计算机图像也被发送,它在etsy商店鄙视。这里真正的问题是什么?提前谢谢。

如果您想了解更多内容。请评论。

1 个答案:

答案 0 :(得分:0)

Etsy不支持localhost图片网址,因此您首先全局上传此图片(在Amazon S3等上)。

<image_link>
 open(https://test.s3.amazonaws.com/wordpress/wp-content/uploads/2013/06/hoodie_2_front.jpg)
</image_link>