我已经建立了一个自定义的api使用php,这是一个简单的api,通过发布xml数据的工作。我要发布到api的代码是:
<?php
$xml_data = '<document>
<first>'.$first.'</first>
<last>'.$last.'</last>
<email>'.$email.'</email>
<phone>'.$phone.'</phone>
<body>TEST</body>
</document>';
$URL = "url";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$Response = curl_exec($ch);
curl_close($ch);
echo "Responce= ".$responce;
?>另一方面,上面的代码发布到:
<?php
$postdata = file_get_contents("php://input");
$xml = simplexml_load_string($postdata);
$first = $xml->first;
$last = $xml->last;
$email = $xml->email;
$phone = $xml->phone;
?>然后,我将这些php变量发送到数据库中。所有这些代码都在工作!!
但我的问题是:我如何将响应发送回发帖端?如何使用curl_init发送到curl_exec?
任何帮助都是最好的!谢谢你,杰森
发布于 2013-01-08 03:49:59
我认为你想要:
echo "Responce= ".$Response;
^^^发布于 2013-01-08 04:17:01
要返回一个响应,您可以像处理任何其他内容一样,设置一个头并回显您的输出。例如,要从处理post数据的脚本返回xml响应,请执行以下操作
<?php
$postdata = file_get_contents("php://input");
$xml = simplexml_load_string($postdata);
$first = $xml->first;
$last = $xml->last;
$email = $xml->email;
$phone = $xml->phone;
// do your db stuff
// format response
$response = '<response>
<success>Hello World</success>
</response>';
// set header
header('Content-type: text/xml');
// echo xml identifier and response back
echo chr(60).chr(63).'xml version="1.0" encoding="utf-8" '.chr(63).chr(62);
echo $response;
exit;
?>您应该会看到从curl_exec()返回的响应
https://stackoverflow.com/questions/14202917
复制相似问题