发送response-phpapi
我使用php构建了一个自定义api,它是一个通过发布xml数据来工作的简单api。 我要发布到api的代码是:发送response-phpapi,php,xml,api,Php,Xml,Api,我使用php构建了一个自定义api,它是一个通过发布xml数据来工作的简单api。 我要发布到api的代码是: <?php $xml_data = '<document> <first>'.$first.'</first> <last>'.$last.'</last> <email>'.$email.'</email> <phone>'.$phone.'</phone>
<?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\u init发送到curl\u exec
任何帮助都会很好!非常感谢。
杰森我想你想要:
echo "Responce= ".$Response;
^^^
我想你想要:
echo "Responce= ".$Response;
^^^
要返回响应,您可以像对任何其他内容一样,设置标题并回显输出。例如,要从处理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\u exec()
返回的响应。要返回响应,您可以像对任何其他内容一样,设置标题并回显输出。例如,要从处理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\u exec()
返回的响应,为什么要调用curl\u exec
两次?错误,抱歉!我取出输出和结束,留下响应和结束。你为什么要调用curl\u exec
两次?错误,抱歉!我取出输出和结束,留下响应和结束。