• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PHP卷曲和XML不工作

用户头像
it1352
帮助1

问题说明

我想有使用curl发送API调用idibu(工作板公司在英国)。我已经成功地做他们的测试信息,他们所提供的电话。您可以看到,这里(我已经编辑并删除了散列键但在实际code它的存在):

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=

add

no
email
bob@bob.com
yes
http://www.谷歌.com 
warning


<![CDATA[XML v 3 test, please ignore Special  $ & % @ ! ? . , = ) ( - : ; _ + ' " ]]>
ABC123456789
Special Te  $ & % @ ! ? . , = ) ( / - : ; _ + ' " ' here euro  
Basic Te  $ & % @ ! ? . , = ) ( / - : ; _ + ' " '  
Special Te  $ & % @ ! ? . , = ) ( / - : ; _ + ' " '  

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

    '        , 	,         ' : '          ' : ' '    h      ]]>

Steve
Rogers
test@mail.net
One World Market
44 (0) 111-1111111
http://uk.idibu.com 
UK
020 1111 1111





2
2011-11-26
Full time
20000
25000

Salary override test test
GBP
2011-11-26


3




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

然后我去codeD这里的数据:的http://$c$crstoolbox.net/string/#!encoding=xml&action=en$c$c&charset=us_ascii 。并得到了完整的XML写出来所以它更容易理解的(并且这样我就可以进入我自己的变量进行处理。我想发布的XML,因为它是(在德codeD格式),但可以'不像是会得到它的工作这是我已经试过:

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.谷歌.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   />
<location   />
<sublocation   />
<jobtype   />
<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函数,它读取的有效载荷,但给我的错误,如标题标签缺失等是明显存在的。正因为如此,我在盘算我需要做一些EN code的信息之前,我卷曲它。

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.

任何帮助越来越指出了正确的方向将是极大的AP preciated。谢谢!

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

正确答案

#1

您应该使用rawurlen code,而不是ヶ辆,只有在有效载荷的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);

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkjji
系列文章
更多 icon
同类精品
更多 icon
继续加载