POST使用cURL不同的数据类型

时间:2012-10-08 11:38:09

标签: php xml curl kml

我生成了一些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”]);返回空?

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

1 个答案:

答案 0 :(得分: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);