Tuesday, 19 February 2019

Zoho response returning false

I created this class to be make working with zoho easier and as far as I can tell everything is correct

<?
    class ZohoWebAPI {
        private $credentials = array(
            "authtoken" => ''
        );

        private $URLS = array(
            "Base" => "https://crm.zoho.com/crm/private/xml/",
            "Contacts" => "Contacts/",
            "Leads" => "Leads/"
        );

        private $methods = array(
            "Insert" => "insertRecords",
            "Update" => "updateRecords",
            "Get" => "getRecords"
        );

        function GetNewAuthToken($loginInfo){
            $url = "https://accounts.zoho.com/apiauthtoken/nb/create?SCOPE=ZohoCRM/crmapi&EMAIL_ID=".$loginInfo['Email']."&PASSWORD=".$loginInfo['Password']."&DISPLAY_NAME=" . $loginInfo['Display_Name'];
            $ch = curl_init($url);
            # Setting our options
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            # Get the response
            $response = curl_exec($ch);
            curl_close($ch);
            $returnArray = explode(" ",$response);
            $res = explode("=",$returnArray[5]);
            $stripToken = str_replace("RESULT","",$res[1]);
            return array(
                "AuthToken" => $stripToken,
                "Result" => $res[2]
            );
        }

        function SetAuthToken($token){
            $this->credentials["authtoken"] = $token;
        }

        function GenerateXML($path,$dataArray){
            $path = strtolower($path);

            $xmlData = '';

            switch($path){
                case "contacts":
                    $xmlData .= "<Contacts>";
                    break;
                case "leads":
                    $xmlData .= "<Leads>";
                    break;
            }

            $xmlData .= "<row no='1'>";

            for($i = 0; $i < count($dataArray);$i++){
                $xmlData .= "<FL val='".$dataArray[$i][0]."'>".$dataArray[$i][1]."</FL>";
            }

            $xmlData .= "</row>";

            switch($path){
                case "contacts":
                    $xmlData .= "</Contacts>";
                    break;
                case "leads":
                    $xmlData .= "</Leads>";
                    break;
            }

            return $xmlData;
        }

        function CreateNewContact($xmlData){
            $apiUrl = $URLS["Base"] . $URLS["Contacts"] . $methods["Insert"];
            $postData = "authtoken" . $credentials["authtoken"] . "&scope=crmapi&xmldata=" . $xmlData;

            return $this->SendDataToZoho($apiUrl,$postData);
        }

        function SendDataToZoho($apiUrl,$postData){
            $ch = curl_init($apiUrl);
            # Setting our options
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            # Get the response
            $response = curl_exec($ch);
            curl_close($ch);

            return $response;
        }
    }
?>

Using this new class I have another file that looks like this

<?
    require_once("zohoapiwrapper.php");

    $zoho = new ZohoWebAPI();

    $zoho->SetAuthToken("a9xxxxxxxxxxxxxxxxxxxxxxxxxxx");

    $dataArray = array(
        array("FirstName","Joseph"),
        array("Last Name","Williamson"),
        array("Email","Testemail@gamilll.com")
    );

    $xml = $zoho->GenerateXML("contacts",$dataArray);

    $result = $zoho->CreateNewContact($xml);

    $responseData = simplexml_load_string($result);

    var_dump($responseData);
?>

When running the code it says (bool)false which does not make sense from what I understand going through the method of adding a contact into the crm the url returns a xml document which would be stored in the $response in the class SendDataToZoho()

So on the line return $this->SendDataToZoho($apiUrl,$postData); I am expecting a xml reponse that could then be parsed to see weather the data has been successfully inserted into zoho or not. However I do not understand where (bool)flase is coming from because if I put the url in a browser and run the generatedXML to it I recieved a xml response from the browser

I am confused and unaware of why it is behaving in this fashion



from Zoho response returning false

No comments:

Post a Comment