首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zoho响应返回false

Zoho响应返回false
EN

Stack Overflow用户
提问于 2019-02-14 17:12:33
回答 1查看 123关注 0票数 1

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

代码语言:javascript
复制
<?
    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;
        }
    }
?>

使用这个新类,我有另一个文件,如下所示

代码语言:javascript
复制
<?
    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);
?>

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

因此,在return $this->SendDataToZoho($apiUrl,$postData);行上,我期望xml能够被解析,以查看数据是否已经成功地插入到zoho中。但是,我不明白(bool)flase是从哪里来的,因为如果我将url放在浏览器中并运行generatedXML到它,就会收到来自浏览器的xml响应。

我很困惑,也不知道它为什么会以这种方式行事。

编辑:

将我的SendToZoho函数更改为

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-19 15:26:52

尝试替换下一个函数:

代码语言:javascript
复制
function CreateNewContact($xmlData){
    $apiUrl = $URLS["Base"] . $URLS["Contacts"] . $methods["Insert"];
    $postData = "authtoken" . $credentials["authtoken"] . "&scope=crmapi&xmldata=" . $xmlData;

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

在这方面:

代码语言:javascript
复制
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 --对对象的引用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54695712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档