0

我生成了一些 xml(实际上是 google kml 文件)

$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://earth.google.com/kml/2.1">';
$kml[] = ' <Document>';

我把它串起来

$kmlOutput = implode("\n", $kml); //Join array elements with a string

然后我做一个数组

$data = array('code' => 'somecode', 'xml' => $kmlOutput);

然后我发送我的 cURL 数据

// 设置 cURL

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$output = curl_exec($ch);

/**
 * Check for some errors (omitted here)
 */
curl_close($ch);

Upload.php 在另一台服务器上

/* MAIN CODE*/
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
    print "\n\n";
    echo "DEBUG: Vardump Post"; 
    var_dump($_POST);
    echo "DEBUG:";
    var_dump($_FILES);

    if ($_FILES["file"]["type"] == "text/xml"  && ($_FILES["file"]["size"] < $maxSize) ){
        if ($_FILES["file"]["error"] > 0){
            echo "Error: (Return Code: " . $_FILES["file"]["error"] . ")<br />";
        } else {
            echo "Code: ".$_POST['key']. "<br />";
            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
            echo "Type: " . $_FILES["file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
            echo "Temp Stored in: " . $_FILES["file"]["tmp_name"]. "<br />"; // temporary copied files disappears when the script ends


            move_uploaded_file($_FILES["file"]["tmp_name"],$dir . $_FILES["file"]["name"]);
            echo "Stored in: " . $dir . $_FILES["file"]["name"];

但是 var_dump($_FILES); 甚至 var_dump($_FILES["code"]); 返回空?

-我在这里错过了什么吗?- 我可以发送两种类型的数据吗?

4

1 回答 1

1

你无法通过它,$_FILES因为你没有上传任何文件

要上传文件,您需要使用@符号后跟文件的位置

 $data = array('code' => 'somecode', 'xml' => "@". $PATH_TO_XML_FILE);

也可以这样直接上传

curl_setopt($ch, CURLOPT_POSTFIELDS, $kmlOutput);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));

例子

$xml =   __DIR__ ."/Pages.xml"; //full part to XML
$data = array('code' => 'somecode', 'xml' => "@$xml");
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$output = curl_exec($ch);
echo $output ;

在远程页面上

    var_dump($_POST,$_FILES);
于 2012-10-08T11:43:53.127 回答