-->

PrestaShop create customer

2020-06-21 04:10发布

问题:

I have this code where I am trying to create a new customer on my website using prestashop mode. But I keep getting error in the response

NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"Login" ofType:@"xml"];

    NSString *xmlStr = [[NSString alloc] initWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil];

     NSString *encodedurlstring = (__bridge NSString*) CFURLCreateStringByAddingPercentEscapes (NULL, (__bridge CFStringRef) xmlStr, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);

    NSString *urlStr = [NSString stringWithFormat:@"http://passkey:@farma-web.it/api/customers/?Xml=%@",encodedurlstring];

    NSURL *webURL = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:webURL];

    [request setHTTPMethod:@"POST"];
    [request setValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *response = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"response - %@",response);

The XML that I have attached is

<prestashop>

<customers>

<customer>**I DO NOT KNOW WHAT TO WRITE HERE**</customer>

<email>abc@abc.com</email>

<passwd>12344321</passwd>

<firstname>ABC</firstname>

<lastname>DEF</lastname>

</customers>

</prestashop>

The response that I am getting is

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<message><![CDATA[Internal error. To see this error please display the PHP errors.]]></message>
</error>
</errors>
</prestashop>

回答1:

"customer" is not an independant field but a container for all the other fields like firstname, lastname, email, etc.

The best way to create a customer is to retrieve and blank sheet and to fill it with your data: http://your-prestashop.com/api/customers?schema=blank



回答2:

I ran into similar issues and here's what I found out on prestashop version 1.6.0.9:

1) As @adrien-g points out, change from define('_PS_MODE_DEV_', false); to define('_PS_MODE_DEV_', true); http://doc.prestashop.com/display/PS16/Setting+Up+Your+Local+Development+Environment#SettingUpYourLocalDevelopmentEnvironment-Displayingerrormessages

2) Next you will start seeing more meaningful errors like the one simulated with this CURL command:

$ curl -kv https://myprestashop.bitnamiapp.com/prestashop/api/customers?ws_key=secret -d '
<?xml version="1.0" encoding="UTF-8"?><customer><lastname>blue</lastname><firstname>boy</firstname><email>boy@blue.com</email></customer>'
... 
<message><![CDATA[parameter "passwd" required]]></message>
...

3) Then some experimentation like adding the passwd and prestashop tags will finally lead you down a path where you see customers successfully being created:

$ curl -kv https://myprestashop.bitnamiapp.com/prestashop/api/customers?ws_key=secret -d '
<?xml version="1.0" encoding="UTF-8"?>
  <prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <customer><lastname>blue</lastname><firstname>boy</firstname><email>boy@blue.com</email>
      <passwd>mysecret</passwd>
    </customer>
  </prestashop>'
... 
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer><id><![CDATA[3]]></id>...</customer></prestashop>
...

Worth noting:

  • using <?xml ...><prestashop></prestashop> versus xml=<?xml ...><prestashop></prestashop> doesn't make any difference in the version I played around with.
  • using <prestashop> versus <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> doesn't make any difference in the version I played around with.


标签: prestashop