Web services php code to add a lead fails silently

I'd appreciate some help to identify why this php code fails silently.

try
{
//define connection options, this allows for recording messages sent and received
$options = array('trace' => 1);

$client = new SoapClient("">cloud.eu.sagecrm.com/.../webservice.wsdl", $options);

//username and password for sage (the user has to have web service access enabled)
$login_details = array('username' => '------------',
'password' => 'xxxxxxxxxxxxxx');

//login to sage
$response = $client->logon($login_details);

//create header to send on future requests
$header = "<sessionheader><sessionid>".$response->result->sessionid."</sessionid></sessionheader>";
$session_var = new SoapVar($header, XSD_ANYXML, null, null, null);
$session_header = new SoapHeader('http://tempuri.org/type', 'SessionHeader', $session_var);

//apply header to client
$client->__setSoapHeaders(array($session_header));

echo "b1";

//define lead data (or pull in from elsewhere), this is only some of the fields
//I will go into further detail on the fields in another blog post
$lead_data = array('companyname' => 'company name',
'companyaddress1' => 'address1',
'personfirstname' => 'first name',
'personlastname' => 'last name',
'status' => 'In Progress');

//create soap variable to send
$lead = new SoapVar($lead_data, XSD_ANYTYPE, "lead", "">http://tempuri.org/type");

//send request to sage
$response = $client->add(array('entityname' => 'lead', 'records' => $lead));

//check response
if (isset($response->result->records->crmid))
{
//worked, the lead id can be found in $response->result->records->crmid
echo "ok";
}
else
{
//failed, display
echo "RESPONSE:\n" . $client->__getLastResponse() . "\n";
}
}
catch (Exception $e)
{
//something went wrong, display request and response
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
echo "RESPONSE:\n" . $client->__getLastResponse() . "\n";

echo $e->getMessage();
}

Thanks.

  • 0

    At what point does it fail?

    At the logon or the add?

  • 0

    Echo statements show that it is the line:

    $client = new SoapClient("">cloud.eu.sagecrm.com/.../webservice.wsdl", $options);

    that is causing the script to fail. Firebug shows no errors. It looks like the WSDL address is invalid - I cannot access the WSDL, this causes a 404 error.

    Thanks.

  • 0

    The WSDL should be able to be addressed just using the a browser address bar to check its validity.

    The pattern is going to be

    https://cloud.eu.sagecrm.com/[instancename]/eware.dll/webservices/CRMwebservice.wsdl

    But the wsdl is not normally necessary for a live program to access. The WSDL just supplies the definitions and once those are know and if the program adheres to the rules then the request can be sent to the web services endpoint.

    The connection string will look like

    https://cloud.eu.sagecrm.com/[instancename]/eware.dll/webservices/

    If you just request this using a 'get' in the browser address bar then you'll only get a blank document - no XML or anything. The end point expects requests to be submitted as HTTP POST transactions.

  • 0

    This makes me wonder.....

    Do you have Professional or Standard edition?

  • 0

    Thanks Jeff.

    View the WSDL

    This is the url I've used for our CRM instance:


    https://cloud.eu.sagecrm.com/MDayWeek457unhaacm8Ij/eware.dll/webservices/CRMwebservice.wsdl

    but I get a 400 error - which indicates the URL is valid but there is some permission issue stopping me for seeing the WSDL XML. Please help me out here a tell me what I need to do to resolve this so I can view the WSDL.

    Run the Test Script


    Changing the code based on what you write above makes no difference - the code still fails at the same line - namely -

    $client = new SoapClient("">cloud.eu.sagecrm.com/.../webservices", $options);

    This script is strongly based on code posted on this forum that is supposed to work. Please can you help by pointing out what is wrong with the php code posted. Or alternatively post some php code that will work either to retrieve XML or JSON data or to insert/amend.

    Thanks.

  • 0

    OK

    I am going to assume that you have checked

    Administration -> System -> Web Services and made sure that

    Enable Web Service = Yes

    ....

    If that is the case then I think you need to add another step to 'spin up' the server and make sure that the web services are available.

    There is an end point

    cloud.eu.sagecrm.com/getcrmurls

    To which you pass an encrypted authorization header. See: community.sagecrm.com/.../sage-crm-39-s-restful-api-sdata-for-cloud-and-on-premise-part-3-of-10.aspx

    You know the SOAP end point (connection string) so this additional call would be a precursor that would wake up the server and allow you to access the system.

  • 0

    Professional I believe because earlier this year someone told me that all trials are professional. Also I have installed the Outlook connector and I don't think you can do that in the basic version.

    But if you tell me how, I'll check - or, as you have our test instance ID, perhaps you could have a look.

  • 0

    Peter

    The article I linked to showed how the geturl() endpoint is called. I wrote the article in the context of using SData but it is very important for SOAP usage too. The 400 error occurs because nobody is logged on and so the instance of the cloud edition has not been loaded into memory. The SOAP web services rely on the eWare.dll being deployed and in memory.

    Anything that has to do with webservices and cloud requires the getcrmurls url. Otherwise the undeployed instances won’t be fired.

    I am sorry but I just don't use PHP in my daily work. I am not comfortable 'correcting' or 'commenting' about the code on that external site. But I can see that the code was written for on premise web services and therefore there is no need to spin up the virtual server. The eware.dll is immediately available for on-premise instances of Sage CRM.

    Once the instance of Sage CRM has been instantiated by the geturls call you will then be able to request the wsdl or just direct calls towards the endpoint (connection string).

  • 0

    Thanks Jeff for your continued input on this.

    Web Services : Yes, they are enabled.

    Your Reference Document - Part 3 of 10:

    I've looked at it but I'm not sure how it helps with the problem of getting the nusoap soapclient declaration to work - it is this statement that is killing the script posted above. I want to stick with nusoap as it is code that we have used successfully with other CRM's.

    Please can you correct the $client statement in the original script that was posted. This script is posted at blog.webma.co.uk/.../connecting-to-sage-crm-with-php.html and a posted comment confirms the script worked. But it an old post 2008 - things may well have changed.

    .

    Access to the WSDL: Surely it should be straightforward to provide a url that displays the Sage CRM WSDL as XML or via a user friendly interface that allows each of the members to be inspected ? I would have thought this is a useful test that the trial CRM instance is set up correctly for Web Services. I can't see how any SOAP code can work if it also cannot read the WSDL.

    Purpose of all this:

    Evaluation of Sage CRM alongside other CRM systems where robust read write API access is a must.

    Availability of PHP Sample Code:

    The only article I can find in this forum is written by you community.sagecrm.com/.../web-services-in-other-programming-languages.aspx in 2008. In my view it would be good to post a current example of a php script that uses the SOAP api to retrieve and update data. Solving the problem in this post would meet this need.

  • 0

    Thanks Jeff, what you write explains a lot. I will now go back and look at your article, which repeats some of the points in the two videos that you recorded and that we have studied. Thank you for your honesty over php expertise.

    If this client proceeds with Sage then it makes me wonder if SOAP with php is the correct tool to be use. Maybe we should be thinking C# - presumably this would lead to more comprehensive paid and forum support ?

    With this in mind can you supply a C# script which we could fire up to demonstrate the API with our test CRM instance ? It could be really simple such as inserting a single lead record and retrieving all contacts as xml or json. Such a script would not need to be 'production robust' simply a demonstration.

  • 0

    I've implemented the approach suggested in community.sagecrm.com/.../sage-crm-39-s-restful-api-sdata-for-cloud-and-on-premise-part-3-of-10.aspx . And this enables me to generate a CRM instance specific url which lets me see CRM entities like Accounts, Persons, Leads and Opportunities. Fantastic !

    Here is my CRM instance specific url for accessing companies:

    cloud.eu.sagecrm.com/sdata[Key from Base64decode]/sagecrm/-/company

    [Key from Base64Decode] is the key obtained, as your article describes, from www.base64decode.org . I supplied the CRM instance user name and password and it generates the key. I then used the REST tool from one of the browsers, again as per your article, and use the base64decode key to create the application header. The string, as your graphic shows, is:

    Authorization:Basic [Key from Base64code]]

    When I click the send button in the browser REST client ( as a GET with the above header ] the Sage CRM URL is one of the lines returned - marked with sData url...

    sDataUrl: "cloud.eu.sagecrm.com/sdata[--- instance specific string ---]/sagecrm/"

    For some strange reason I need to add to the url /-/ and then the db entity which I want to examine e.g.

    cloud.eu.sagecrm.com/sdata[Key from Base64decode]/sagecrm/-/company

    This returns XML for 10 companies ( although the test system now has around 100 ) . All this is brilliant.

    However I need some further help:

    If I run this url in another browser (i.e. not the one with the REST Client that I used to generate the keys then I get, in the case of the url with entity company at the end, a feed list with a single line for each company. However the list isn't clickable and I cannot therefore see the full XML.

    If I use the url in a different browser window to the one used to generate the keys then, despite the url having key information in it, I have to supply a user name and password via a pop-up dialogue box. If I give this data then I see the XML.

    In all of this I only see a small set of companies

    So here are the questions:

    1. What do I have to do to encapsulate the user name and password into code ( C# if you wish) so that I can take the url that works fine in the browser REST tool and produce the xml without a user login ? As you will recall my original objective is to build a working example where code is run that generates xml or json that can be parsed and used for input to another system.

    2. How can I get all records not just 10 ?

    3. How can I see the table structure for the CRM database so I know that the xml returned is giving me all the data and also am aware of all of the objects where xml can be created ? As we now seem to be using REST not SOAP this won't be the WSDL but there must be some XML documentation setting out the methods and data structures exposed to the REST interface.

    4. Is REST now the preferred protocol to use with the API ?

    I sense we are nearing the winning post on this so I hope you can continue to help with answers to the points above. Thanks for your help so far.

  • 0

    I can help with a sample project. It is one that mostly use for on - premise training but it should fine with cloud.

    Can you see this? community.sagecrm.com/.../27223.aspx

  • 0

    Hi Jeff

    No, I get a resource not found message,

  • 0

    the above code works in php, i wonder though how to call the logoff in php anyone to help here ?

  • 0

    The basic structure in XML of a logout request is

    [code language="xml"]
    143126394341507143126394341507
    [/code]

    Alternatively you could send the request

    localhost/.../logout