Jump to content

Access to prestashop webservice via .NET application


alolika

Recommended Posts

  • 4 weeks later...

Imports System.Net

Imports System.Xml

Imports System.IO

Imports System.Text

Imports System.Web

 

Public Class Prestashop_WebClient

 

Private WebService_URL As String = "http://jss/prestashop/api"

Private WebService_LoginName As String = "UPKES78K0ODWAY7L1PO5OZQAYJIIACZZ"

Private WebService_Password As String = ""

 

Private Class CRUDMethod

Public Shared Create As String = "POST"

Public Shared Read As String = "GET"

Public Shared Update As String = "PUT"

Public Shared Delete As String = "DELETE"

End Class

 

Private Function ReadRecord(ByVal TableName As String, ByVal UniqueID As String) As String

Try

Dim RequestURL As String = WebService_URL & "/" & TableName & "/" & UniqueID

Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(RequestURL), HttpWebRequest)

webRequest.Method = CRUDMethod.Read

webRequest.ContentType = "application/xml"

webRequest.Credentials = New NetworkCredential(WebService_LoginName, WebService_Password)

 

Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)

Dim reader As StreamReader = New StreamReader(response.GetResponseStream())

Dim strData As String = reader.ReadToEnd()

reader.Close()

Return strData

Catch ex As Exception

MsgBox("ReadRecord-" & ex.Message)

Return ""

End Try

End Class

  • Like 2
Link to comment
Share on other sites

  • 6 months later...

I have solved it. The problem was that I was testing on the "product" table, and it has many fields, so the header was too big for Apache server. With all the other tables it works fine. Now the question is how to fix it for "product" table. I have post it here: http://www.prestashop.com/forums/topic/157696-webservice-and-default-limit-request-fieldsize/

 

I hope to find a solution soon.

Link to comment
Share on other sites

  • 6 months later...

Hi,I found your post I would like to change order states from my program written in Visual Basic. I remade your code (a quick version):

 



       Dim webRequest1 As WebRequest = WebRequest.Create("http://prestashop/api/order_histories?schema=blank")
       webRequest1.Credentials = New NetworkCredential(WebService_LoginName, WebService_Password)
       webRequest1.ContentType = "application/xml"
       ' Set the Method property of the request to POST.
       webRequest1.Method = "POST"

       ' Create POST data and convert it to a byte array.
       Dim postData As String = "<?xml version='1.0' encoding='UTF-8'?><prestashop xmlns:xlink='http://www.w3.org/1999/xlink'><order_history> <id><![CDATA[94]]></id> <id_order_state xlink:href='http:/prestashop/api/order_states/6'><![CDATA[1]]></id_order_state> <id_order xlink:href='http://prestashop/api/orders/52'><![CDATA[1]]></id_order> <id_employee><![CDATA[1]]></id_employee> <date_add readonly='true'><![CDATA[2012-09-16 21:19:01]]></date_add> <date_upd readonly='true'><![CDATA[2012-09-16 21:19:01]]></date_upd></order_history></prestashop>"
       Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
       ' Set the ContentType property of the WebRequest.
       ' webRequest1.ContentType = "application/x-www-form-urlencoded"
       ' Set the ContentLength property of the WebRequest.
       webRequest1.ContentLength = byteArray.Length
       ' Get the request stream.
       Dim dataStream As Stream = webRequest1.GetRequestStream()
       ' Write the data to the request stream.
       dataStream.Write(byteArray, 0, byteArray.Length)
       ' Close the Stream object.
       dataStream.Close()
       ' Get the response.
       Dim response As WebResponse = webRequest1.GetResponse()
       ' Display the status.
       Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
       ' Get the stream containing content returned by the server.
       dataStream = response.GetResponseStream()
       ' Open the stream using a StreamReader for easy access.
       Dim reader As New StreamReader(dataStream)
       ' Read the content.
       Dim responseFromServer As String = reader.ReadToEnd()
       ' Display the content.
       Console.WriteLine(responseFromServer)
       ' Clean up the streams.
       reader.Close()
       dataStream.Close()
       response.Close()


 

But I get an error "The remote server returned an error: (500) Internal Server Error."in line " Dim response As WebResponse = webRequest1.GetResponse()"

Could you please help me. What do I do wrong? Could you maybe provide me with your code?

Thanks for help in advance

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

My answer using C#, hope it help you

 

HttpWebRequest PedirPagina = (HttpWebRequest)WebRequest.Create("http://192.168.1.206/prestashop/api/order_histories");
	    NetworkCredential nc = new NetworkCredential("KZ2CYY3HMMZZO84YB8TSQZATFBPI5BUB", "");
	    PedirPagina.Credentials = nc;
	    PedirPagina.Method = "POST";
	    PedirPagina.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
	    byte[] byteArray = Encoding.UTF8.GetBytes("xml=" +
		    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
		    "<prestashop xmlns:xlink=\"http://www.w3.org/1999/xlink\">"+
		    "<order_history><id_order_state>7</id_order_state><id_order>7</id_order><id_employee>1</id_employee></order_history>" +
		    "</prestashop>");

	    PedirPagina.ContentLength = byteArray.Length;
	    Stream dataStream = PedirPagina.GetRequestStream();
	    dataStream.Write(byteArray, 0, byteArray.Length);

	    dataStream.Close();

	    HttpWebResponse RespuestaPagina = (HttpWebResponse)PedirPagina.GetResponse();
	    if (PedirPagina.HaveResponse) // si hay respuesta
	    {
		    //obtener contenido de la respuesta
		    using (Stream streamContenido = RespuestaPagina.GetResponseStream())
		    {
			    MessageBox.Show(new StreamReader(streamContenido).ReadToEnd());
		    }
	    }
	    RespuestaPagina.Close();

  • Like 1
Link to comment
Share on other sites

  • 8 months later...
  • 7 months later...
  • 3 weeks later...
  • 1 year later...

Greetings,

I am a new user of Prestashop and I am trying to sync the Products table with my ERP's items.

I am able to insert items, however I have 2 issues, and I was wondering if someone could give me a hint.

 

1) How can I define the product stock? When I insert a procuct, it automatically creates a record like: 

<stock_available xlink:href="http://example.com/api/stock_availables/2892">

 

When I follow the link, it has fields for stock. How can I access that and have control over it when creating an item?

 

2) How can I update an item? When I use the <id>555</id> to update the 555 item, it throws a bad request error. I try it both with POST and PUT requests. Is there another way?

 

Thank you 

Link to comment
Share on other sites

  • 2 months later...

Hello,

 

I'm new of prestashop I would like to use prestasharp, and I have try to use, but I have alway an error of "401 Unautorized". Have you any idea?

 

 

 

    public class Prestashop_WebClient
    {
        private string WebService_URL = "http://www.xxxxxxxxxxxxxxxxxxxxxx.com/eshop/api";
        private string WebService_LoginName = "I22R8VQ91YR2KGPHYVPA4I7TU661CPIM     ";
        private string WebService_Password = "";

        public Prestashop_WebClient()
        {

        }

        public string ReadRecord(string TableName, string UniqueID)
        {
            try{
                string RequestURL = WebService_URL + "/" + TableName; // +"/" + UniqueID;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(RequestURL);
                webRequest.Credentials = new NetworkCredential(WebService_LoginName, WebService_Password);
                webRequest.Method = CRUDmethod.Read;
                webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string strData = reader.ReadToEnd();
                reader.Close();
                return strData;
            } catch(Exception ex)
            {
                MessageBox.Show("ReadRecord-" + ex.Message);
                return "";
            }
        }

        public string WriteRecord(string TableName, string UniqueID)
        {
            try
            {
                string RequestURL = WebService_URL + "/" + TableName; // +"/" + UniqueID;
                //products/1
                string BaseUrl = "http://www.xxxxxxxxxxxxxxxxxxx.com/eshop/api/";
                string Account = "I22R8VQ91YR2KGPHYVPA4I7TU661CPIM";
                string Password = "";

                ManufacturerFactory ManufacturerFactory = new ManufacturerFactory(BaseUrl, Account, Password);
                Bukimedia.PrestaSharp.Entities.manufacturer Manufacturer = ManufacturerFactory.Get(162); //162 = ofba; 5 = acer
                Manufacturer.name = "Iron Maiden";
                Manufacturer.active = 1;
                ManufacturerFactory.Add(Manufacturer);
                ManufacturerFactory.Update(Manufacturer);
                ManufacturerFactory.Delete(Manufacturer);

                ProductFactory ProductShop = new ProductFactory(BaseUrl, Account, Password);
                Bukimedia.PrestaSharp.Entities.product MyProduct = new Bukimedia.PrestaSharp.Entities.product();
                MyProduct = ProductShop.Add(MyProduct);


                BaseUrl = "http://www.xxxxxxxxxxxxxxxxxxxxxxxxx.com/eshop/api/images";
                 Account = "I22R8VQ91YR2KGPHYVPA4I7TU661CPIM";
                Password = "";
                ImageFactory ProductImageShop = new ImageFactory(BaseUrl, Account, Password);
                ProductImageShop.AddProductImage((long)MyProduct.id, "C:\\MyImage.jpg");
              

Edited by massimopasquali (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Hello,

 

I have seen all yours examples, and like you have seen, I have chosen prestasharp. Well, my problem persisted, also, when I tried to access at my webservice using any browser, I can't, becouse Presta reuturned me the error "401 Unauthorized". Why? :blink:
Had someone resolved this problem?

Edited by massimopasquali (see edit history)
Link to comment
Share on other sites

  • 4 months later...
  • 1 month later...

a mi me sale este error,

 

No se controló System.Net.WebException
  HResult=-2146233079
  Message=Error en el servidor remoto: (401) No autorizado.
 
a que se debe?? yo creo que es necesario el uso de
Public ReadOnly Property Status As WebExceptionStatus
para que no haya error en el control
VB
Edited by Roy-FELL (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

hello!

error server 400

The following code

Dim TableName As String = "languages"
            Dim RequestURL As String = WebService_URL & TableName & "?schema=blank"
            Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(RequestURL), HttpWebRequest)
            webRequest.Credentials = New NetworkCredential(WebService_LoginName, WebService_Password)
            webRequest.ContentType = "application/xml"
            webRequest.Method = CRUDMethod.Create
            
            Dim MyXML As String = "xml=<?xml version=""1.0"" encoding=""UTF-8""?>" & _
                "<prestashop xmlns:xlink=""http://www.w3.org/1999/xlink"">" & _
                "<language><id><![CDATA[99]]></id>" & _
                "<name><![CDATA[italiano (Italian)]]></name>" & _
                "<iso_code><![CDATA[it]]></iso_code>" & _
                "<language_code><![CDATA[it-it]]></language_code>" & _
                "<active><![CDATA[1]]></active><is_rtl><![CDATA[0]]></is_rtl>" & _
                "<date_format_lite><![CDATA[d/m/Y]]></date_format_lite>" & _
                "<date_format_full><![CDATA[d/m/Y H:i:s]]></date_format_full>" & _
                "</language></prestashop>"

Dim byteArray As Byte() = Encoding.UTF8.GetBytes(MyXML)
            webRequest.ContentLength = byteArray.Length
            Dim dataStream As Stream = webRequest.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse, HttpWebResponse)

 

at this point the program goes wrong, what is wrong?
please help me
thank you

Link to comment
Share on other sites

  • 3 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...