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

时间:2012-03-31 20:32:08

标签: api crm zoho

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

API文档位于http://zohocrmapi.wiki.zoho.com/insertRecords-Method.html

当通过网络浏览器地址栏传递时,它可以工作:

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>

使用file_get_contents运行时,我没有遇到任何错误。有谁知道我需要做些什么才能让它发挥作用?

4 个答案:

答案 0 :(得分:3)

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

<?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类型。

答案 1 :(得分:2)

   <?php
$xml = 
        '<?xml version="1.0" encoding="UTF-8"?>
        <Contacts>
        <row no="1">
        <FL val="First Name">Digant</FL>
        <FL val="Last Name">Shah1</FL>
        <FL val="Email">digant.shah91@gmail.com</FL>
        <FL val="Department">php</FL>
        <FL val="Phone">999999999</FL>
        <FL val="Fax">99999999</FL>
        <FL val="Mobile">99989989</FL>
        <FL val="Assistant">none</FL>
        </row>
        </Contacts>';
$auth="*******************";
    $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;




?>

答案 2 :(得分:1)

如果您遇到SSL连接问题,请尝试添加下一个卷曲选项:

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

答案 3 :(得分:0)

`$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