首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发送响应- PHP API

发送响应- PHP API
EN

Stack Overflow用户
提问于 2013-01-08 03:45:09
回答 2查看 233关注 0票数 0

我已经建立了一个自定义的api使用php,这是一个简单的api,通过发布xml数据的工作。我要发布到api的代码是:

代码语言:javascript
复制
<?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;
?>

另一方面,上面的代码发布到:

代码语言:javascript
复制
<?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?

任何帮助都是最好的!谢谢你,杰森

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-08 03:49:59

我认为你想要:

代码语言:javascript
复制
 echo "Responce= ".$Response;
                   ^^^
票数 2
EN

Stack Overflow用户

发布于 2013-01-08 04:17:01

要返回一个响应,您可以像处理任何其他内容一样,设置一个头并回显您的输出。例如,要从处理post数据的脚本返回xml响应,请执行以下操作

代码语言:javascript
复制
<?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()返回的响应

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14202917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档