Zoho的回应是假的

问题描述 投票:1回答:1

我创建这个类是为了让你更容易使用zoho,据我所知,一切都是正确的

<?
    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;
        }
    }
?>

使用这个新类我有另一个看起来像这样的文件

<?
    require_once("zohoapiwrapper.php");

    $zoho = new ZohoWebAPI();

    $zoho->SetAuthToken("a9xxxxxxxxxxxxxxxxxxxxxxxxxxx");

    $dataArray = array(
        array("FirstName","Joseph"),
        array("Last Name","Williamson"),
        array("Email","[email protected]")
    );

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

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

    $responseData = simplexml_load_string($result);

    var_dump($responseData);
?>

当运行代码时,它说(bool)false从我理解通过将联系人添加到crm的方法没有意义,url返回一个xml文档,该文档将存储在类$response中的SendDataToZoho()

所以在线return $this->SendDataToZoho($apiUrl,$postData);我期待一个xml响应,然后可以解析看天气数据已成功插入zoho或不。但是我不明白(bool)flase的来源,因为如果我把url放在浏览器中并运行generatedXML,我会从浏览器中收到一个xml响应

我很困惑,也不知道为什么它会以这种方式表现

编辑:

将我的SendToZoho功能更改为此

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);
            $oh1 = curl_error($ch);
            $oh2 = curl_errno($ch);
            curl_close($ch);

            var_dump($oh1 . " " . $oh2);

            return $response;
        }

这是输出string(17) " malformed 3" bool(false)

php zoho
1个回答
2
投票

尝试替换下一个功能:

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

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

有了这个:

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

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

问题是如果你想引用一个对象的属性 - 你应该使用$this - 对象的引用。

© www.soinside.com 2019 - 2024. All rights reserved.