使用PHP通过API向Zoho CRM插入记录

使用PHP通过API向Zoho CRM插入记录,api,crm,zoho,Api,Crm,Zoho,我认为使用get_file_contents函数可以让我像执行过去使用的其他API一样执行API。然而,这种方法不适用于Zoho CRM API——可能是因为我传递的是XML数据,而不是RESTful查询 API文档位于 通过web浏览器地址栏传递此信息时,它会起作用: https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?authtoken=Auth Token&scope=crmapi &newFormat=1

我认为使用get_file_contents函数可以让我像执行过去使用的其他API一样执行API。然而,这种方法不适用于Zoho CRM API——可能是因为我传递的是XML数据,而不是RESTful查询

API文档位于

通过web浏览器地址栏传递此信息时,它会起作用:

https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?authtoken=Auth Token&scope=crmapi
&newFormat=1
&xmlData=
<Contacts>
<row no="1">
<FL val="First Name">Scott</FL>
<FL val="Last Name">James</FL>
<FL val="Email">test@test.com</FL>
<FL val="Department">CG</FL>
<FL val="Phone">999999999</FL>
<FL val="Fax">99999999</FL>
<FL val="Mobile">99989989</FL>
<FL val="Assistant">John</FL>
</row>
</Contacts>
https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?authtoken=Auth 令牌&scope=crmapi
&newFormat=1
&XML数据=
斯科特
詹姆斯
test@test.com
CG
999999999
99999999
99989989
约翰

使用文件内容运行此文件时,我没有收到任何错误。有人知道我需要做什么才能让它工作吗?

如果SSL连接有问题,请尝试添加下一个curl选项:

`$ch = curl_init('https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?');
curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response) 
curl_setopt($ch, CURLOPT_POST, 1);//Regular post 
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha');

如果SSL连接有问题,请尝试添加下一个curl选项:

curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha');

这对我有用。我写了两个方法。一个用于获取身份验证密钥,另一个用于创建联系人

<?php 
class zoho{

    public function getAuth()
    {
        $username = "youremail@mail.com";
        $password = "yourpassword";
        $param = "SCOPE=ZohoCRM/crmapi&EMAIL_ID=".$username."&PASSWORD=".$password;
        $ch = curl_init("https://accounts.zoho.com/apiauthtoken/nb/create");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $result = curl_exec($ch);
        /*This part of the code below will separate the Authtoken from the result.
        Remove this part if you just need only the result*/
        $anArray = explode("\n",$result);
        $authToken = explode("=",$anArray['2']);
        $cmp = strcmp($authToken['0'],"AUTHTOKEN");
        echo $anArray['2'].""; if ($cmp == 0)
        {
        echo "Created Authtoken is : ".$authToken['1'];
        return $authToken['1'];
        }
        curl_close($ch);
    }   



public function postData($auth, $fornavn,$efternavn, $email,$addr,$by,$postnr,$land,$kommentar)
    {
        $xml = 
        '<?xml version="1.0" encoding="UTF-8"?>
        <Contacts>
        <row no="1">
        <FL val="First Name">'.$fornavn.'</FL>
        <FL val="Last Name">'.$efternavn.'</FL>
        <FL val="Email">'.$email.'</FL>
        <FL val="Department">'.$land.'</FL>
        <FL val="Phone">999999999</FL>
        <FL val="Fax">99999999</FL>
        <FL val="Mobile">99989989</FL>
        <FL val="Assistant">none</FL>
        </row>
        </Contacts>';

    $url ="https://crm.zoho.com/crm/private/xml/Contacts/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
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;

    }
}

?>

这样使用:

<?php 
    include('zoho.php');
    $zoho = new zoho();
    echo "testing....<br />";
    $auth = $zoho->getAuth();
    echo " <pre>";
    echo $auth;
    $result = $zoho->postData($auth, 'Bob','test', 'lol@lol.dk','adresse','by','postr','Danmark','Some comment');
    print_r($result);
 ?>


我仍在研究在联系人上插入地址信息所需的XML格式。

这对我很有用。我写了两个方法。一个用于获取身份验证密钥,另一个用于创建联系人

<?php 
class zoho{

    public function getAuth()
    {
        $username = "youremail@mail.com";
        $password = "yourpassword";
        $param = "SCOPE=ZohoCRM/crmapi&EMAIL_ID=".$username."&PASSWORD=".$password;
        $ch = curl_init("https://accounts.zoho.com/apiauthtoken/nb/create");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $result = curl_exec($ch);
        /*This part of the code below will separate the Authtoken from the result.
        Remove this part if you just need only the result*/
        $anArray = explode("\n",$result);
        $authToken = explode("=",$anArray['2']);
        $cmp = strcmp($authToken['0'],"AUTHTOKEN");
        echo $anArray['2'].""; if ($cmp == 0)
        {
        echo "Created Authtoken is : ".$authToken['1'];
        return $authToken['1'];
        }
        curl_close($ch);
    }   



public function postData($auth, $fornavn,$efternavn, $email,$addr,$by,$postnr,$land,$kommentar)
    {
        $xml = 
        '<?xml version="1.0" encoding="UTF-8"?>
        <Contacts>
        <row no="1">
        <FL val="First Name">'.$fornavn.'</FL>
        <FL val="Last Name">'.$efternavn.'</FL>
        <FL val="Email">'.$email.'</FL>
        <FL val="Department">'.$land.'</FL>
        <FL val="Phone">999999999</FL>
        <FL val="Fax">99999999</FL>
        <FL val="Mobile">99989989</FL>
        <FL val="Assistant">none</FL>
        </row>
        </Contacts>';

    $url ="https://crm.zoho.com/crm/private/xml/Contacts/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
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;

    }
}

?>

这样使用:

<?php 
    include('zoho.php');
    $zoho = new zoho();
    echo "testing....<br />";
    $auth = $zoho->getAuth();
    echo " <pre>";
    echo $auth;
    $result = $zoho->postData($auth, 'Bob','test', 'lol@lol.dk','adresse','by','postr','Danmark','Some comment');
    print_r($result);
 ?>

我仍在研究在联系人上插入地址信息所需的XML格式。




$authtoken=“7D3E488297BB4EC00B457CFF3292196”$xmlData='Scott Jamestest@test.comCG 999999999999999999999999999999999989989约翰'$query=“newFormat=1&authtoken={$authtoken}&scope=crmapi&xmlData={$xmlData}”;curl_setopt($ch,CURLOPT_POSTFIELDS,$query)$响应=curl_exec($ch);回音$应答;卷曲关闭($ch)$authtoken=“7D3E488297BB4EC00B457CFF3292196”$xmlData='Scott Jamestest@test.comCG 999999999999999999999999999999999989989约翰'$query=“newFormat=1&authtoken={$authtoken}&scope=crmapi&xmlData={$xmlData}”;curl_setopt($ch,CURLOPT_POSTFIELDS,$query)$响应=curl_exec($ch);回音$应答;卷曲关闭($ch);现在我知道这是个坏主意。不要对每个请求都请求authtoken。如果您当前的密钥不起作用,请创建一个请求新密钥的方法,而不是在其中硬编码密钥,甚至更好。现在有一个方法可以在2020年处理此问题。现在我知道这是一个坏主意。不要对每个请求都请求authtoken。如果您当前的密钥不起作用,请创建一个请求新密钥的方法,而不是在那里硬编码密钥,甚至更好。现在有一个方法可以在2020年处理此问题。感谢@Digant Shah您救了我的命,并澄清了CRM的概念,非常感谢!!!!谢谢@Digant Shah你救了我的命,也澄清了CRM的概念,非常感谢!!!!