0

POST リクエストをリッスンする小さな PHP スクリプトがあります。私は常にxmlを期待しています。通常、私は xml リクエストを送信する側です。でも今日は受け取る側です。

$_POST をリッスンする単純なケースだと思いましたが、間違っている可能性があります。何も得られません。

xml を待機するスクリプトは次のとおりです。

<?php
if(isset($_POST)) {
    mail("me@myemail.com","some title i want", print_r($_POST, true)); 
}else{
    die("uh, what happened?");
}
?>

そして、これは私が別の場所から送信している単純な xml 文字列です。

<?php
$xml_data ='
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don\'t forget me this weekend!</body>
</note>
';

function sendXML2Server($URL,$XML){
    $xml_data = trim($XML);
    $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_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);

    return $output;
}

echo sendXML2Server('https://someurl.com/inboundxml.php',$xml_data)
?>

そして、これが私のメールで得たものです:

配列 ( )

私は配列を正しく操作していないと推測していますが、これには他に何か欠けているものがあるかもしれません。実際のxml文字列が返されることを期待しています。

4

2 に答える 2

1

データのみを送信するため、PHP はこのデータをキーと値として解釈できません。したがって、変数の値として送信する必要があります。

curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml_data' => $xml_data));

または未加工の投稿データとして受け取る:

<?php
if(isset($HTTP_RAW_POST_DATA)) {
    mail("me@myemail.com","some title i want", print_r($HTTP_RAW_POST_DATA, true)); 
}else{
    die("uh, what happened?");
}
?>
于 2012-07-10T01:22:22.643 に答える
0

CURLOPT_POSTFIELDS には配列が必要です:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('content'=>$xml_data));

次に、次のように取得します。

<?php
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['content'])) {
    mail("me@myemail.com","some title i want", print_r($_POST['content'], true)); 
}else{
    die("uh, what happened?");
}
?>
于 2012-07-10T01:23:24.873 に答える