带有 XML 的 PHP cURL 不起作用 [英] PHP cURL with XML Not Working

查看:41
本文介绍了带有 XML 的 PHP cURL 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 cURL 向 idibu(英国的一家求职公司)发送 API 调用.我已经使用他们提供的测试信息成功拨打了电话.你可以在这里看到(我已经编辑并删除了哈希键,但在实际代码中它就在那里):

I'm trying to have use cURL to send an API call to idibu (a job board company in the UK). I've successfully made the call with their test information that they provide. You can see that here (I've edited and removed the hash key but in the actual code it's there):

<?php
/**
 * Define POST URL and also payload
 */
define('XML_PAYLOAD', 'xml_text=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0D%0A%3Cidibu%3E%0D%0A%3Cmethod%3Eadd%3C%2Fmethod%3E%0D%0A%3Cconfig%3E%0D%0A%3Cshow_durations%3Eno%3C%2Fshow_durations%3E%0D%0A%3Ccompletionurl%3Eemail%3C%2Fcompletionurl%3E%0D%0A%3Cadvertcompletionemail%3Ebob%40bob.com%3C%2Fadvertcompletionemail%3E%0D%0A%3Clockboards%3Eyes%3C%2Flockboards%3E%0D%0A%3Credirecturl%3Ehttp%3A%2F%2Fwww.google.com+%3C%2Fredirecturl%3E%0D%0A%3Cvalidate_level%3Ewarning%3C%2Fvalidate_level%3E%0D%0A%3C%2Fconfig%3E%0D%0A%3Cjob%3E%0D%0A%3Ctitle%3E%3C%21%5BCDATA%5BXML+v+3+test%2C+please+ignore+Special+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+-+%3A+%3B+_+%2B+%27+%22+%80%5D%5D%3E%3C%2Ftitle%3E%0D%0A%3Creference%3EABC123456789%3C%2Freference%3E%0D%0A%3Cdescription%3E%3C%21%5BCDATA%5B%3Cb%3ESpecial+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+here+euro+%80+%0D%0ABasic+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0ASpecial+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0A%0D%0A+%0D%0A+Special+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0ABasic+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0A%0D%0A+%96+%96+%92+%27+%95+++++++%2C+%09%2C+%95+%B7+%93+%94+%D8+%85+%A8+%A6+%27+%3A+%27+%83+%E1+%91+%BA+%A1+%A7+%E9+%D7+%87+%27+%3A+%27+%27+%C7+%BD+%84+h+%AE+%97+%AC+%83+%E1+%99%3C%2Fb%3E%5D%5D%3E%3C%2Fdescription%3E%0D%0A%3Csender%3E%0D%0A%3Cname%3ESteve%3C%2Fname%3E%0D%0A%3Clastname%3ERogers%3C%2Flastname%3E%0D%0A%3Cemail%3Etest%40mail.net%3C%2Femail%3E%0D%0A%3Ccompany%3EOne+World+Market%3C%2Fcompany%3E%0D%0A%3Cphone%3E44+%280%29+111-1111111%3C%2Fphone%3E%0D%0A%3Cwww%3Ehttp%3A%2F%2Fuk.idibu.com+%3C%2Fwww%3E%0D%0A%3Ccountry%3EUK%3C%2Fcountry%3E%0D%0A%3Cpostcode%3E020+1111+1111%3C%2Fpostcode%3E%0D%0A%3C%2Fsender%3E%0D%0A%3Ccategory+id%3D%2221%22+%2F%3E%0D%0A%3Clocation+id%3D%228%22+%2F%3E%0D%0A%3Csublocation+id%3D%22668%22+%2F%3E%0D%0A%3Cjobtype+id%3D%222%22+%2F%3E%0D%0A%3Cjob_time%3E2%3C%2Fjob_time%3E%0D%0A%3Cstartdate%3E2011-11-26%3C%2Fstartdate%3E%0D%0A%3Cduration%3EFull+time%3C%2Fduration%3E%0D%0A%3Csalarymin%3E20000%3C%2Fsalarymin%3E%0D%0A%3Csalarymax%3E25000%3C%2Fsalarymax%3E%0D%0A%3Csalaryper+value%3D%22annum%22+%2F%3E%0D%0A%3CsalaryOverride%3ESalary+override+test+test%3C%2FsalaryOverride%3E%0D%0A%3Ccurrency%3EGBP%3C%2Fcurrency%3E%0D%0A%3Cpublish%3E2011-11-26%3C%2Fpublish%3E%0D%0A%3Cposts%3E%0D%0A%3Cboard+id%3D%22517%22%3E%0D%0A%3Cextrafield+name%3D%22idibudts_cat%22%3E3%3C%2Fextrafield%3E%0D%0A%3Cduration+days%3D%227%22+%2F%3E%0D%0A%3C%2Fboard%3E%0D%0A%3C%2Fposts%3E%0D%0A%3C%2Fjob%3E%0D%0A%3C%2Fidibu%3E');
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);
?>

然后我在这里解码数据:http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=us_ascii .并写出完整的 XML 以便更容易理解(这样我可以输入我自己的变量进行处理.我想按原样发布 XML(以解码格式)但似乎无法得到它可以工作.这是我尝试过的:

I then decoded the data here: http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=us_ascii . And got the full XML written out so it's more easily understandable (and so I can enter my own variables in to be processed. I'd like to POST the XML as it is (in the decoded format) but can't seem to get it to work. Here's what I've tried:

<?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);
?>

在将其定义为 XML_PAYLOAD 时,我也尝试过在 '$xml' 周围不加单引号.两次我都得到:

I've also tried it without the single quotes around '$xml' when defining it as the XML_PAYLOAD. Both times I get:

"error":"The xml payload is missing"

当我在没有 htmlentities 函数的情况下尝试它时,它会读取有效负载,但会给我错误,例如缺少 TITLE 标签"等,这显然存在.正因为如此,我想在我卷曲它之前我需要对信息进行一些编码.

When I try it without the htmlentities function, it reads the payload but gives me errors like 'the TITLE tag is missing', etc. which is clearly there. Because of this, I'm figuring I need to do some encode of the information before I cURL it.

任何指向正确方向的帮助将不胜感激.谢谢!

Any help getting pointed in the right direction would be greatly appreciated. Thanks!

推荐答案

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

You should be using rawurlencode instead of htmlentities, and only on the XML part of the 'payload'. Also, you're defining your constant to the literal text "$xml" by using single quotes around your variable. So replacing those two lines with these should do:

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

define('XML_PAYLOAD', $xml);

这篇关于带有 XML 的 PHP cURL 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆