我正在尝试将一个LinkedIn注册连接到Zoho的招聘API中
当我使用requestbin时,它看起来应该可以工作,但是当我提交给zoho时,我得到一个错误,无法解析数据类型。
这是关于如何将xml post构造为zoho的api的信息
https://www.zoho.com/recruit/add-records.html
下面是我的代码。对我做错了什么有什么建议吗?
<?php
$post_body = file_get_contents('php://input');
$application = json_decode($post_body);
//shortcodes
$firstname = $application->person->firstName;
$lastname = $application->person->lastName;
$city = $application->location->name;
$email = $application->person->emailAddress;
$headline = $application->person->headline;
/*
* XML Sender/Client.
*/
// Get our XML. You can declare it here or even load a file.
$xml_builder = "
<Candidates>
<row no=\"1\">
<FL val=\"First name\">{$firstname}</FL>
<FL val=\"Last name\">{$lastname}</FL>
<FL val=\"Contact address\">{$lastname}</FL>
<FL val=\"Email ID\">{$email}</FL>
<FL val=\"Current job title\">{$headline}</FL>
</row>
</Candidates>
";
// Initialize curl
$curl = curl_init();
$opts = array(
CURLOPT_URL => 'https://recruit.zoho.com/ats/private/xml/Candidates/addRecords?authtoken=#secrettoken&scope=recruitapi&duplicateCheck=1&xmlData={$xml_builder}',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $xml_builder,
CURLOPT_HTTPHEADER => array('Content-Type: text/xml','Content-Length: ' . strlen($xml_builder))
);
// Set curl options
curl_setopt_array($curl, $opts);
// Get the results
$result = curl_exec($curl);
// Close resource
curl_close($curl);
echo $result;
$fp = fopen('zoho.txt', 'w');
fwrite($fp, $result);
fclose($fp);
?>
最佳答案
必须指定postfields名称。在您发送的文档(https://www.zoho.com/recruit/add-records.html)之后,您应该从url中删除xmlData
和duplicateCheck
参数,这是一个post-only api。
我认为您还应该添加xml声明(<?xml version='1.0' standalone='yes'?>
)
因此,curlopt_postfields定义的代码如下:
$xml_builder = array(
'duplicateCheck' => 1 ,
'xmlData' => "
<?xml version='1.0' standalone='yes'?>
<Candidates>
<row no=\"1\">
<FL val=\"First name\">{$firstname}</FL>
<FL val=\"Last name\">{$lastname}</FL>
<FL val=\"Contact address\">{$lastname}</FL>
<FL val=\"Email ID\">{$email}</FL>
<FL val=\"Current job title\">{$headline}</FL>
</row>
</Candidates>"
);
$opts数组应该如下所示:
$opts = array(
CURLOPT_URL =>'https://recruit.zoho.com/ats/private/xml/Candidates/addRecords?authtoken=#secrettoken&scope=recruitapi',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $xml_builder
);