0

私はphpを使用してカスタムAPIを構築しました。これは、xmlデータを投稿することで機能する単純なAPIです。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 に送信するにはどうすればよいですか?

どんな助けでも素晴らしいでしょう!ありがとうジェイソン

4

2 に答える 2

2

私はあなたが欲しいと思います:

 echo "Responce= ".$Response;
                   ^^^
于 2013-01-07T19:49:59.223 に答える
1

応答を返すには、他のコンテンツの場合と同じように、ヘッダーを設定して出力をエコーし​​ます。たとえば、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()

于 2013-01-07T20:17:01.257 に答える