vasilist Posted August 30, 2011 Share Posted August 30, 2011 Has anyone upload an image to Prestashop through web services? I'm trying to do it using c# but until now there is no result.. If someone has an example-sample just try post it.. I don't care about language(c#, vbnet, php, etc)! Thanks Link to comment Share on other sites More sharing options...
vasilist Posted September 6, 2011 Author Share Posted September 6, 2011 After a lot of try, i found a solution.. I don't like it very much, but it's a start private void UploadImage(byte[] content, PrestaShop.Model.Product product) { PrestaShop.WebService.PrestaShopApi api = PrestashopHelper.CreateNewApiObject(); string url = api.url; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url + "/images/products/" + product.Id); request.KeepAlive = true; request.Credentials = new NetworkCredential(api.key, ""); request.Method = "POST"; string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); request.ContentType = "multipart/form-data; boundary=" + boundary; string postData = "--" + boundary + "\nContent-Disposition: form-data; name=\"image\"; filename=\"test.jpg\";\r\nContent-Type: image/pjpeg\r\n\r\n"; byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData); //create recuest content request.ContentLength = postDataBytes.Length + content.Length + boundarybytes.Length; request.GetRequestStream().Write(postDataBytes, 0, postDataBytes.Length); request.GetRequestStream().Write(content, 0, content.Length); request.GetRequestStream().Write(boundarybytes, 0, boundarybytes.Length); WebResponse response = null; try { response = request.GetResponse(); } catch { } response.Close(); } Link to comment Share on other sites More sharing options...
xuri Posted September 10, 2011 Share Posted September 10, 2011 Where i can get this api for c#?? Thanks Link to comment Share on other sites More sharing options...
vasilist Posted September 12, 2011 Author Share Posted September 12, 2011 Where i can get this api for c#?? Thanks Unfortunately, i haven't find something yet.. So I'm developing it, on my own... Link to comment Share on other sites More sharing options...
rmaz Posted October 10, 2011 Share Posted October 10, 2011 Hello, This sounds good, but what is the value of the variable "content"? thank you Link to comment Share on other sites More sharing options...
vasilist Posted October 11, 2011 Author Share Posted October 11, 2011 It's the Image binary.. Load the image to a stream and pass it to "content" as Array.. check here http://www.codeproject.com/KB/recipes/ImageConverter.aspx Link to comment Share on other sites More sharing options...
rmaz Posted October 12, 2011 Share Posted October 12, 2011 Hello, Thank you for the answer, but your code really works because I work in vb.net and I can not adapt your code I have a 500 error every time, can you help me or give me your email address by private Message? Thank you in advance Dim fs As FileStream = New FileStream("C:\Users\btsig\Desktop\romain.jpg", FileMode.Open) Dim br As BinaryReader = New BinaryReader(fs) Dim content() As Byte = br.ReadBytes(fs.Length) br.Close() Dim request As HttpWebRequest = HttpWebRequest.Create("http://xxxx.com/api/images/products/1") request.KeepAlive = True request.Credentials = New NetworkCredential("key_api", "") request.Method = "POST" Dim boundary As String = "----------------------------" + DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture) Dim boundarybytes As Byte() = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n") request.ContentType = "multipart/form-data; boundary=" + boundary Dim postData As String = "--" + boundary + "\nContent-Disposition: form-data; name=\""image\""; filename=\""test.jpg\"";\r\nContent-Type: image/pjpeg\r\n\r\n" Dim postDataBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(postData) 'create recuest content request.ContentLength = postDataBytes.Length + content.Length + boundarybytes.Length request.GetRequestStream().Write(postDataBytes, 0, postDataBytes.Length) request.GetRequestStream().Write(content, 0, content.Length) request.GetRequestStream().Write(boundarybytes, 0, boundarybytes.Length) request.GetResponse( Link to comment Share on other sites More sharing options...
vasilist Posted October 13, 2011 Author Share Posted October 13, 2011 If I rememder right, you should use Dim postData As String = "--" + boundary + "\nContent-Disposition: form-data; name=""image""; filename=""test.jpg"";\r\nContent-Type: image/pjpeg\r\n\r\n" instead of Dim postData As String = "--" + boundary + "\nContent-Disposition: form-data; name=\""image\""; filename=\""test.jpg\"";\r\nContent-Type: image/pjpeg\r\n\r\n" (remove backslashes) Try it, and if it doesn't work PM me Link to comment Share on other sites More sharing options...
rmaz Posted October 20, 2011 Share Posted October 20, 2011 Hello, I sent you a private message because I don't have the solution and I do not know where to look for resolve my problem.. Thank you for your help. Link to comment Share on other sites More sharing options...
roland_d_alsace Posted March 21, 2012 Share Posted March 21, 2012 Hi. This topic is old sure, but there are not a lot topics with this subject. Do you know how can i have the picture ID after uploading. In effect with PS 1.4.7 you mut to make a product association afte pictures uploading and some other actions. PS Webservice API don't make this alone After uploading Prestashop send one more time the picture in response after uploding (1.4.7) and send nothing else (no xml). This is not very interesting like response..... Regards Thanks. Regards. Link to comment Share on other sites More sharing options...
symphonyx Posted April 18, 2012 Share Posted April 18, 2012 Hi, here is the way to (it's a bit dirty but works). I didn't find another way to : - Get the product's images xml ( api/images/products/{id_products} - Upload your new image (same url) - Get again the product's images xml - compare the old image listing with the new one, if there is a new entry in your xml, it's probably your new pixture Don't forget to set the default image to your product after that (update product), cause the cover is empty if you don't assign one Regards Link to comment Share on other sites More sharing options...
mrkahvi Posted May 15, 2012 Share Posted May 15, 2012 hi symphonyx, the problem is when i upload new images within new product creation. It's known that new product created still have no image xlink. Link to comment Share on other sites More sharing options...
symphonyx Posted May 15, 2012 Share Posted May 15, 2012 (edited) You have first to create your product and then upload the images in the created product. Do you inderstand or do i have to explain more ? Mayvbe I don't understand what you want to do. Regards Edited May 15, 2012 by symphonyx (see edit history) Link to comment Share on other sites More sharing options...
mrkahvi Posted May 16, 2012 Share Posted May 16, 2012 Thanks for you answer.. First i have created new product with no image using webservice and succeed. Yet both the tag <image> and <id_default_image> in the xml document of such new product still contain no xlink or blank so i dont know where i should upload my image to.. Link to comment Share on other sites More sharing options...
mrkahvi Posted May 16, 2012 Share Posted May 16, 2012 You said that the image should be uploaded to the same url : /api/images/products/{product_id} whilst a new products created through WS still doesnt have this URL. I have tried it and you may try it yourself too Link to comment Share on other sites More sharing options...
symphonyx Posted May 16, 2012 Share Posted May 16, 2012 If you look well in the url `/api/images/products/{product_id}' you'll see that there is a dir call `images' in it. Let me highlight it : `/api/images/products/{product_id}' Is it ok for you ? Have you tested it ? If it doesn't work, maybe it's because you don't have the permission to CRUD this action. To fix that, you have to go fo the webservice rights management in prestashop back-office, and add the product images by checking its CRUD's checkbox. Is that ok ? Link to comment Share on other sites More sharing options...
mrkahvi Posted May 18, 2012 Share Posted May 18, 2012 (edited) I have all permission for all tables... Ok, here is the case what i have done, My presta initially has 8 products with id 1-8. Every products have their own images I'd like to add a new product with image. So.... I first added a product with no images product creation has succeeded with the ID 9 so the product URL is /api/product/9 and this link works I tried to go to /api/images/products/9/ to upload an image and the URL didn't work I then looked at '/api/images/products/' to make sure if the URL '/api/images/products/9/' exists. Yet the URL obviously still doesnt exist. The lasts URL ive seen is '/api/images/products/8' -----> This is what i meant the xlink of such new image still doesnt exist. Also in XML document of the new product, the tag <image> still has no xlink. I think your first tips above assumes that a new image is added into an existing product that have already had an initial image, not a new product that has no image yet. Edited May 18, 2012 by mrkahvi (see edit history) Link to comment Share on other sites More sharing options...
estribiyo Posted May 18, 2012 Share Posted May 18, 2012 I have exactly the same problem... I'm creating new product via webservice, but I have no idea how I can upload an image for this new product... It's not possible? Link to comment Share on other sites More sharing options...
symphonyx Posted May 18, 2012 Share Posted May 18, 2012 (edited) It works for me. I've just check my code. What is the image field name you POST ? The name of your image field must called "image". You must verify too if your image MIME type is correct, and that your request content-type is "application/x-www-form-urlencoded" That is certainely the problem. When you say that it doesn't work. Do the HTTP response send something to you ? an error or something ? Notice that the image can't be bigger than 3Mo. Regards. Edited May 18, 2012 by symphonyx (see edit history) Link to comment Share on other sites More sharing options...
gabryel80 Posted May 25, 2012 Share Posted May 25, 2012 Hi ! I am new at this section of webservice. Please could you tell me if it is possible to create a new product via webservice. I would like to give the possibility to one of my clients to create products in my website. My thoughts are that a form will be enough for this but very hard to create the script . Would you be so kind to help me with a script in order to create a form which will be used by somebody else than administrator to create products in my database. Hope to manage this ..... Thanks in advance !!! Link to comment Share on other sites More sharing options...
symphonyx Posted May 26, 2012 Share Posted May 26, 2012 Hello, have you read the prestashop documentation ? http://doc.prestashop.com You can find there some examples and samples you can use to create your form. If you wanna make a form to create products via webservice, you have know PHP language (or another server side language), or any other language wich can send HTTP request. We usually say : read the f****** manual and the exercices. Than, you can ask for more questions. Regards. 1 Link to comment Share on other sites More sharing options...
srebella Posted May 17, 2013 Share Posted May 17, 2013 If you look well in the url `/api/images/products/{product_id}' you'll see that there is a dir call `images' in it. Let me highlight it : `/api/images/products/{product_id}' Is it ok for you ? Have you tested it ? If it doesn't work, maybe it's because you don't have the permission to CRUD this action. To fix that, you have to go fo the webservice rights management in prestashop back-office, and add the product images by checking its CRUD's checkbox. Is that ok ? Hello i am having same problem, i know it is an old task. Let me explain here what it is happening. First of all my permission of images are: <image_types xlink:href="http://127.0.0.1:81/prestashop/api/image_types" get="true" put="true" post="true" delete="true" head="true"> <description xlink:href="http://127.0.0.1:81/prestashop/api/image_types" get="true" put="true" post="true" delete="true" head="true">The image types</description> <schema xlink:href="http://127.0.0.1:81/prestashop/api/image_types?schema=blank" type="blank"/> <schema xlink:href="http://127.0.0.1:81/prestashop/api/image_types?schema=synopsis" type="synopsis"/> </image_types> <images xlink:href="http://127.0.0.1:81/prestashop/api/images" get="true" put="true" post="true" delete="true" head="true"> <description xlink:href="http://127.0.0.1:81/prestashop/api/images" get="true" put="true" post="true" delete="true" head="true">The images</description> </images> I would like to insert a new image on a recently created product. The Post header looks like this POST /prestashop/api/images/products/1 HTTP/1.1 Content-Length: 43685 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Host: 127.0.0.1:81 Proxy-Connection: Keep-Alive User-Agent: Apache-HttpClient/4.2.1 (java 1.5) Authorization: Basic .. image=0xFFD8FFE000104A46494600010101012C012C0000FFE20C584943435F50524F46494C4500000100000C484C696E6F021000006D6E74725247422058595A2007CE00020009000600310000616373704D5346540000000049454320735247420000000000000000000000000000F6D6000100000000D32D4850202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001163707274000001500000003364657363000001840000006C77747074000001F000000014626B707400000204000000147258595A00000218000000146758595A0000022C000000146258595A0000024000000014646D6E640000025400000070646D6464000002C400000088767565640000034C0000008676696577000003D4000000246C756D69000003F8000000146D6561730000040C0000002474656368000004300000000C725452430000043C0000080C675452430000043C0000080C625452430000043C0000080C7465787400000000436F70797269676874202863292031393938204865776C6574742D5061636B61726420436F6D70616E790000646573630000000000000012735247422049454336313936362D322E31000000000000000000000012735247422049454336313936362D322E31000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000058595A20000000000000F35100010000000116CC58595A200000000000000000000000000000000058595A200000000000006FA2000038F50000039058595A2000000000000062990000B785000018DA58595A2000000000000024A000000F840000B6CF64657363000000000000001649454320687474703A2F2F7777772E6965632E636800000000000000000000001649454320687474703A2F2F7777772E6965632E63680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000064657363000000000000002E4945432036313936362D322E312044656661756C742052474220636F6C6F7572207370616365202D207352474200000000000000000000002E4945432036313936362D322E312044656661756C742052474220636F6C6F7572207370616365202D20735247420000000000000000000000000000000000000000000064657363000000000000002C5265666572656E63652056696577696E6720436F6E646974696F6E20696E2049454336313936362D322E3100000000000000000000002C5265666572656E63652056696577696E6720436F6E646974696F6E20696E2049454336313936362D322E31000000000000000000000000000000000000000000000000000076696577000000000013A4FE00145F2E0010CF140003EDCC0004130B00035C9E0000000158595A2000000000004C09560050000000571FE76D6561730000000000000001000000000000000000000000000000000000028F0000000273696720000000004352542063757276000000000000040000000005000A000F00140019001E00230028002D00320037003B00400045004A004F00540059005E00630068006D00720077007C00810086008B00900095009A009F00A400A900AE00B200B700BC00C100C600CB00D000D500DB00E000E500EB00F000F600FB01010107010D01130119011F0125012B01320138013E0145014C0152015901600167016E0175017C0183018B0192019A01A101A901B101B901C101C901D101D901E101E901F201FA0203020C0214021D0226022F02380241024B0254025D02670271027A0284028E029802A202AC02B602C102CB02D502E002EB02F50300030B03160321032D03380343034F035A03660372037E038A039603A203AE03BA03C703D303E003EC03F9040604130420042D043B0448045504630471047E048C049A04A804B604C404D304E104F004FE050D051C052B053A05490558056705770586059605A605B505C505D505E505F6060606160627063706480659066A067B068C069D06AF06C006D106E306F507070719072B073D074F076107740786079907AC07BF07D207E507F8080B081F08320846085A086E0882089608AA08BE08D208E708FB09100925093A094F09640979098F09A409BA09CF0 What i receive from server is HTTP/1.1 500 Internal Server Error Date: Fri, 17 May 2013 18:51:54 GMT Server: Apache/2.2.22 (Win64) PHP/5.4.3 Vary: Authorization,Host X-Powered-By: PrestaShop Webservice Access-Time: 1368816714 PSWS-Version: 1.5.4.0 Execution-Time: 0.013 Content-Length: 230 Connection: close Content-Type: text/xml;charset=utf-8 <?xml version="1.0" encoding="UTF-8"?> <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <errors> <error> <code><![CDATA[66]]></code> <message><![CDATA[unable to save this image]]></message> </error> </errors> </prestashop> Do you see something missing? If yes/no please reply someone! Link to comment Share on other sites More sharing options...
sadlyblue Posted June 5, 2013 Share Posted June 5, 2013 I'm trying to upload images also (products/categories/...) but with no luck so far. Can someone be so kind and post a working example? Thanks Link to comment Share on other sites More sharing options...
hazaruddin Posted September 18, 2014 Share Posted September 18, 2014 Get image from external link, save it to temp directory and upload via PS webservice. function SetCategoryImage($cat_id, $image_url) { /* Prepare the image filename */ $imgURL = urldecode($image_url); $image_name = (stristr($imgURL,'?',true))?stristr($imgURL,'?',true):$imgURL; $pos = strrpos($image_name,'/'); $image_name = substr($image_name,$pos+1); /* Check the directory. Create if not exist*/ if (!file_exists('/tmp/img/')) mkdir('/tmp/img/', 0777, true); /* Copy image to local temp*/ $ch = curl_init($image_url); $fp = fopen('/tmp/img/'.$image_name, 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); /* Set the local image path*/ $image_path = '/tmp/img/'.$image_name; /* Set the API URL for category*/ $url = PS_SHOP_PATH .'/api/images/categories/'.$cat_id; /* Upload image using CURL */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':'); curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.$image_path.';type=image/jpeg')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now