【问题标题】:receiving xml file via http post通过http post接收xml文件
【发布时间】:2010-08-11 10:51:32
【问题描述】:

我有一个测试脚本来通过 http post 接收一个 xml 文件,当我在内部使用它时它似乎工作正常。当我将脚本移动到可以从外部访问的 Web 服务器时,似乎没有发生任何事情。有人有什么想法吗?

<?php   
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $inp = fopen("php://input"); 
    $outp = fopen("xmlfile" . date("YmdHis") . ".xml", "w"); 
    while (!feof($inp)) { 
        $buffer = fread($inp, 8192); 
        fwrite($outp, $buffer); 
    }        
    fclose($inp); 
    fclose($outp);
    echo "<html><head>test response</head><body>OK</body></html>";
}
?>

要发布我正在使用 curl 的 xml,不确定这是否是问题所在?而且我没有发送到安全连接(HTTPS):

function httpsPost($Url, $xml_data)
{    
   //Initialisation
   $ch=curl_init();

   //Set parameters
   curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
   curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

   curl_setopt($ch, CURLOPT_URL, $Url);

   //Return a variable instead of posting it directly
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   //Activate the POST method
   curl_setopt($ch, CURLOPT_POST, 1);

   //Request   
   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
   curl_setopt($ch, CURLOPT_TIMEOUT, 4);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

   //execute the connexion
   $result = curl_exec($ch);

   //Close it
   curl_close($ch); 
   return $result;
 }

【问题讨论】:

    标签: php http-post httppostedfile


    【解决方案1】:

    确保在您的服务器上allow_url_fopen 设置已从 php.ini 开启。

    话虽如此,请注意security concerns 关于该设置。

    更新:

    试试看有没有错误,打开报错,把这两行放在你的脚本上面:

    ini_set('display_errors', true);
    error_reporting(E_ALL);
    

    【讨论】:

    • 嗨,是的,它似乎设置为 allow_url_fopen = On
    • 嗨,谢谢。我已经尝试过似乎会出现任何错误。基本上网络服务器上的脚本只是上面的代码+您的更新。不知道问题是什么!
    • 编辑了问题以显示我是如何发布它的...不确定这是否是一个原因。
    • 你是否一开始就打开了 curl 扩展?
    • 是的...它确实适用于另一个 Web 应用程序,并且当我发布到 localhost 时它正在工作。虽然我放了例如回声“test1”;在上面的接收脚本中,没有任何内容被回显……这就是为什么我想知道它是否被正确发布。可以尝试以其他方式发布。
    【解决方案2】:

    需要检查的其他事项:

    1. 如果表单有enctype=multipart/form-data,则php://input 不可用
    2. php://input 只能读取一次(不太可能,除非您的脚本还有其他部分未显示)
    3. POST 数据大小不超过 Apache 的 LimitRequestBody 和/或 PHP 的 upload_max_size/post_max_size

    您有什么理由必须阅读原始输入并且基本上不能做到fwrite($outp, $_POST['xml'])

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      相关资源
      最近更新 更多