不使用XML的PHP cURL

不使用XML的PHP cURL,php,xml,api,curl,html-entities,Php,Xml,Api,Curl,Html Entities,我正在尝试使用cURL向idibu(英国的一家招聘委员会公司)发送一个API调用。我已经用他们提供的测试信息成功地打了电话。您可以在这里看到(我已经编辑并删除了散列键,但在实际代码中它就在那里): 当我在没有htmlentities函数的情况下尝试它时,它会读取有效负载,但会给我诸如“TITLE tag丢失”等错误,这显然是存在的。正因为如此,我想我需要在卷曲之前对信息进行一些编码 如果您能为我们指明正确的方向,我们将不胜感激。谢谢 您应该使用rawurlencode而不是htmlentitie

我正在尝试使用cURL向idibu(英国的一家招聘委员会公司)发送一个API调用。我已经用他们提供的测试信息成功地打了电话。您可以在这里看到(我已经编辑并删除了散列键,但在实际代码中它就在那里):

当我在没有htmlentities函数的情况下尝试它时,它会读取有效负载,但会给我诸如“TITLE tag丢失”等错误,这显然是存在的。正因为如此,我想我需要在卷曲之前对信息进行一些编码


如果您能为我们指明正确的方向,我们将不胜感激。谢谢

您应该使用rawurlencode而不是htmlentities,并且只在“负载”的XML部分使用。此外,通过在变量周围使用单引号将常量定义为文本“$xml”。因此,用这两行替换这两行应该可以:

$xml = 'xml_text='.rawurlencode('<?xml … </idibu>'); # <-- insert entire XML there

define('XML_PAYLOAD', $xml);

$xml='xml\u text='.rawurlencode('您不需要对文章正文进行编码。还可以尝试设置内容类型标题。应该是text/xml完成了。太棒了。谢谢您,我已经为最后一块拼图绞尽脑汁很久了。谢谢!
<?php
/**
 * Define POST URL and also payload
 */
$xml = htmlentities('xml_text=<?xml version="1.0" encoding="UTF-8"?>
<idibu>
<method>add</method>
<config>
<show_durations>no</show_durations>
<completionurl>email</completionurl>
<advertcompletionemail>bob@bob.com</advertcompletionemail>
<lockboards>yes</lockboards>
<redirecturl>http://www.google.com </redirecturl>
<validate_level>warning</validate_level>
</config>
<job>
<title><![CDATA[XML v 3 test, please ignore Special £ $ & % @ ! ? . , = ) ( - : ; _ + ]]></title>
<reference>ABC123456789</reference>
<description><![CDATA[<b>Special Te £ $ & % @ ! ? . , = ) ( / - : ; _ +  "  here euro  
Basic Te £ $ & % @ ! ? . , = ) ( / - : ; _ + "   
Special Te £ $ & % @ ! ? . , = ) ( / - : ; _ +  "   


 Special Te £ $ & % @ ! ? . , = ) ( / - : ; _ +  "   
Basic Te £ $ & % @ ! ? . , = ) ( / - : ; _ +  "   

            ,   ,  ·   Ø  ¨ ¦  :   á  º ¡ § é ×   :   Ç ½  h ®  ¬  á </b>]]></description>
<sender>
<name>Steve</name>
<lastname>Rogers</lastname>
<email>test@mail.net</email>
<company>One World Market</company>
<phone>44 (0) 111-1111111</phone>
<www>http://uk.idibu.com </www>
<country>UK</country>
<postcode>020 1111 1111</postcode>
</sender>
<category id="21" />
<location id="8" />
<sublocation id="668" />
<jobtype id="2" />
<job_time>2</job_time>
<startdate>2011-11-26</startdate>
<duration>Full time</duration>
<salarymin>20000</salarymin>
<salarymax>25000</salarymax>
<salaryper value="annum" />
<salaryOverride>Salary override test test</salaryOverride>
<currency>GBP</currency>
<publish>2011-11-26</publish>
<posts>
<board id="517">
<extrafield name="idibudts_cat">3</extrafield>
<duration days="7" />
</board>
</posts>
</job>
</idibu>');

define('XML_PAYLOAD', '$xml');
define('XML_POST_URL', 'http://ws.idibu.com/clients/api/REMOTE/V3/[INSERT HASH KEY]');

/**
 * Initialize handle and set options
 */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

/**
 * Execute the request and also time the transaction
 */
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);

$xml = simplexml_load_string($result);
$json = json_encode($xml);
$arr = json_decode($json,true);

$temp = array();
foreach($arr as $k=>$v) {
  foreach($v as $k1=>$v1) {
    $temp[$k][$k1] = $v1;
  }
}

$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;

/**
 * Check for errors
 */
if ( curl_errno($ch) ) {
    $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
    $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    switch($returnCode){
        case 404:
            $result = 'ERROR -> 404 Not Found';
            break;
        default:
            break;
    }
}

/**
 * Close the handle
 */
curl_close($ch);

/**
 * Output the results and time
 */
echo 'Total time for request: ' . $totalTime . "\n";
echo $json;  

/**
 * Exit the script
 */
exit(0);
?>
"error":"The xml payload is missing"
$xml = 'xml_text='.rawurlencode('<?xml … </idibu>'); # <-- insert entire XML there

define('XML_PAYLOAD', $xml);