3

我正在尝试将链接的注册连接到 Zoho 的 Recruit API 的附加内容

当我使用 requestbin 时,它似乎应该可以工作,但是当我提交给 Zoho 时,我得到一个错误,无法解析数据类型。

这是有关如何将 xml 帖子构建到 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);  



?>
4

2 回答 2

2

您必须指定 POSTFIELDS 名称。按照您发送的文档 ( https://www.zoho.com/recruit/add-records.html ),您应该从 URL中删除xmlData和参数,这是一个仅限 POST 的 API。duplicateCheck

我认为您还应该添加 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                                                                   
);
于 2013-09-17T11:01:23.290 回答
0

现在你已经给

CURLOPT_URL             => 'https://recruit.zoho.com/ats/private/xml/Candidates/addRecords?authtoken=#secrettoken&scope=recruitapi&duplicateCheck=1&xmlData={$xml_builder}',

您可以如下所示将单引号转换为双引号并尝试一次

CURLOPT_URL             => "https://recruit.zoho.com/ats/private/xml/Candidates/addRecords?authtoken=#secrettoken&scope=recruitapi&duplicateCheck=1&xmlData={$xml_builder}",
于 2013-09-17T12:06:57.347 回答