2012-03-31 101 views
1

我認爲使用get_file_contents函數可以讓我執行API,就像我以前用過的其他API一樣。然而,這種方法不適用於Zoho CRM API--可能是因爲我傳遞的是XML數據而不是RESTful查詢?使用PHP通過API將記錄插入Zoho CRM

API文檔是在http://zohocrmapi.wiki.zoho.com/insertRecords-Method.html

當通過這個通過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">[email protected]</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時,我沒有得到任何錯誤。有誰知道我需要做些什麼才能使其發揮作用?

回答

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 
+0

$ authtoken =「7d3e4882977bb4ec00b457cff3292196」; $ XMLDATA =」 斯科特 詹姆斯 [email protected] CG 999999999 99999999 99989989 John '; $ query =「newFormat = 1&authtoken = {$ authtoken}&scope = crmapi&xmlData = {$ xmlData}」; curl_setopt($ ch,CURLOPT_POSTFIELDS,$ query); $ response = curl_exec($ ch); echo $ response; curl_close($ ch); – user1525978 2012-07-18 05:31:17

1

如果您有SSL連接的煩惱嘗試添加下一個捲曲的選項:

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

這是爲我工作。非常感謝發佈這個! – john 2013-11-15 20:34:26

3

這是爲我工作。我寫了2種方法。一個用於獲取授權密鑰,另一個用於創建聯繫人。

<?php 
class zoho{ 

    public function getAuth() 
    { 
     $username = "[email protected]"; 
     $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', '[email protected]','adresse','by','postr','Danmark','Some comment'); 
    print_r($result); 
?> 

我還在你需要插入的聯絡地址信息什麼樣的XML的工作。

+1

現在我知道這是一個壞主意。不要在每個請求中請求authtoken。相反,在那裏硬編碼密鑰或甚至更好,創建一個方法,請求一個新的密鑰,如果你的當前密鑰不起作用。 – Bolli 2013-03-28 14:30:37

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">[email protected]</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; 




?> 
+0

謝謝@Digant Shah你拯救了我的生命並清除了關於CRM的概念非常感謝! – 2015-05-12 07:14:05