Webservice, OData, PHP modify example?

RamonchuRamonchu Member Posts: 31
Hello.
I'm creating a tool in PHP as a REST interface, with Curl. For testing I've created a webservice for Customer card page. I can read without problems, but when I try to PUT to modify the card of a customer, I get this error:

"Could not validate the client concurrency token required by the service. Please provide a valid token in the client request."

Reading the forum I've seen that I have to read an ID and use that ID to PUT, so NAV can check there are not modifications between operations, but there is no information about how to do it.

Do you have an example about how to modify using PHP and ODATA? I don't know if the problem comes from that ID, from a token, authentication....
Thanks!

Answers

  • MarijnMarijn Member Posts: 69
    When you read a record, there is an additional column added called Bookmark if I remember correctly, which is the information NAV needs to deal with concurrent users. When you change a value and modify the record, that Bookmark is still there and you should not have any problems with that. So make sure you have this information, it's a very long string, and this information is still present when you try to modify it. I do not have my PHP close at hand, but I could look for some examples at a later time.
  • MarijnMarijn Member Posts: 69
    On a second thought, you're using REST. Actually I think that might be your problem. Because this token is one of the params and probably is way too long to be included in a REST request. I never tried that and I probably never will because to me it seems as an odd way of doing things. I think you should use a PHP SOAP client.
  • RamonchuRamonchu Member Posts: 31
    Hello Marijn. Thanks for your help.
    The only field that i see is this:

    ETag: 28;EgAAAAJ7BTAAMAAwADAAMQAAAAAA8;257624890;

    I think that should be the field, so if I send a PUT, do I have to include that tag along with the fields to modify? or where do I have to include that field?

    Example:

    $ch = curl_init();
    $customer = '00001';

    curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Connection: Keep-Alive',
    'Accept: application/json',
    'Content-Type: application/json; charset=utf-8',
    "Accept: */*"
    ]);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERPWD, *myuserandpassword*);
    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'Phone_No' => '123456789',
    'ETag' => "28;EgAAAAJ7BTAAMAAwADAAMQAAAAAA8;257624890"
    ]));
    $api_request_url = "http://192.168.1.19:7048/DynamicsNAV90/OData/Company('TEST')/Customer('$customer')";
    curl_setopt($ch, CURLOPT_URL, $api_request_url);
    $response = json_decode(curl_exec($ch), TRUE);

    With that code I get the error.
    Best regards.
  • MarijnMarijn Member Posts: 69
    Yes, you need to include that field, just like it was part of a primary key. I don't remember the name being ETag. In NAV2009 the token was named Bookmark if I am not mistaken. But I am not sure whether curl is the right tool for the job. If you can read a record, I suppose it can be done. You should be able to read a record. And without any changes, save it right away.
  • MarijnMarijn Member Posts: 69
    Maybe you should put ETag at the top as the very first parameter.
  • RamonchuRamonchu Member Posts: 31
    I've tried both things, in key (where I get a key error) and as first paramenter, but both don't work. I have no example where to put that tag.... no idea.
  • MarijnMarijn Member Posts: 69
    Then I can't help you. I only got experience with PHP calling the NAV webservices with SOAP, not ODATA and REST. I used Freddys Blog as a starting point: https://blogs.msdn.microsoft.com/freddyk/2010/01/19/connecting-to-nav-web-services-from-php/

    Not sure whether that helps you.
  • bobgally9bobgally9 Member Posts: 15
    edited 2016-03-09
    Please use fallowing example for PHP consuming NAV webservices

    https://onedrive.live.com/redir?resid=2D58FE62FC930D3B!933&authkey=!AFmzxXzJGCbCCEc&ithint=folder,zip

    Hope this Helps

    Thanks
    www.moulikaku.com
    Hope this Helps

    Thanks
    Mouli K
    www.moulikaku.com
  • RamonchuRamonchu Member Posts: 31
    edited 2016-03-10
    Hello.
    Thank you for your answers. I already had that zip file for Soap.
    Finally I solved the problem with the kind help of John Slegers (for Odata).
    With Soap I have a php error but I've read I have to activate NTLM authentication in the nav service. I have to test...

    With OData, the ETag is the field that must be read and used to modify or delete. That field must be sent in the header of the "Patch" call (with curl). There is also a curl parameter to include if you use http or https call (for authentication). I will test in deep everything and post the results.

    Thank you for your help! ;)
Sign In or Register to comment.