Search code examples
phpjsonparsingcurlzoho

Parse/Decode response (Json format?) - PHP Curl


I am using PHP Curl to push html form data to a CRM (Zoho), where a record is created.

Response after creating the record (echo $response;):

*Record(s) added successfully*3442526000000497019*2018-09-04 11:32:312018-09-04 11:32:31*

I want to parse/decode this response ($response), to extract the record-Id (3442526000000497019).

In thre CRM API SDK, it says something about responses being in json format.

Therefore, I tried to decode the response with json_decode fucntion:

var_dump(json_decode($responser));
var_dump(json_decode($responser, true));

However, this returns a NULL (NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.) and not an array.

I would appreciate any tip, hint, fingerpoint to a good direction. Do I have to decode it in some other way?

The following extract of my script shows how the form data is inserted (as XML data to the CRM via php Curl):

<?php

$xml =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <Leads>
        <row no=\"1\">
            <FL val=\"Uhrzeit\">".$uhrzeit."</FL>       
            <FL val=\"Datum\">".$datum."</FL>
            <FL val=\"First Name\">'Mad'</FL>
            <FL val=\"Last Name\">'Dog'</FL>
            <FL val=\"Phone\">".$phone."</FL>
            <FL val=\"Email\">".$email."</FL>
            <FL val=\"Zip Code\">".$postcode."</FL>
            <FL val=\"fuerwen\">".$fuerwen."</FL>
            <FL val=\"pflegegrad\">".$pflegegrad."</FL>
            <FL val=\"mobilitaet\">".$mobilitaet."</FL>
            <FL val=\"sprache\">".$sprache."</FL>
            <FL val=\"betreuungsbeginn\">".$zeitpunkt."</FL>
        </row>
    </Leads>";
$auth="fewfwefwe";
    $url ="https://crm.zoho.com/crm/private/xml/Leads/insertRecords";
    $query="authtoken=".$auth."&scope=crmapi&newFormat=1&xmlData=".$xml;
    $ch = curl_init();
    /* set url to send post request */
    curl_setopt($ch, CURLOPT_URL, $url);
    /* allow redirects */
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    /* return a response into a variable */
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    /* times out after 30s */
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    /* set POST method */
    curl_setopt($ch, CURLOPT_POST, 1);
    /* add POST fields parameters */
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.

    //Execute cUrl session
    $responser = curl_exec($ch);
    curl_close($ch);
    echo $responser;

    var_dump(json_decode($responser));
    var_dump(json_decode($responser, true));

Kind regards Leonore


Solution

  • CRM API SDK is implemented to deal with ZOHO CRM API Version 2 and the code you referenced is calling API Version 1.

    You are posting to the XML API so, you should be expecting to receive XML response.

    If you want to have a json response consider changing the XML part to JSON in the API URL like this: https://crm.zoho.com/crm/private/json/Leads/insertRecords and it should be accepting the exactly same parameters as the XML API.

    Here is a sample response:

    {"response":{"result":{"recorddetail":{"FL":[{"val":"Id","content":"3442526888888888888"},{"val":"Created Time","content":"2018-09-11 14:21:31"},{"val":"Modified Time","content":"2018-09-11 14:21:31"},{"val":"Created By","content":"Jon Deo"},{"val":"Modified By","content":"Jon Deo"}]},"message":"Record(s) added successfully"},"uri":"/crm/private/json/Leads/insertRecords"}}
    

    Please, consider removing the authtoken from your question as it is public now!