我创建该类的目的是使与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","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文档,该文档将存储在{{1}中}在$response
因此,在SendDataToZoho()
行上,我期望可以对XML响应进行解析,以查看天气是否已成功将数据插入zoho。但是我不知道return $this->SendDataToZoho($apiUrl,$postData);
的来源,因为如果我将URL放在浏览器中并对其运行生成的XML,则会从浏览器收到xml响应
我很困惑,也不知道为什么它会以这种方式表现
编辑:
将我的(bool)flase
函数更改为此
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;
}
答案 0 :(得分: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
-对对象的引用。