卷曲HTTP发布问题

时间:2018-05-17 09:38:30

标签: php curl post postman php-curl

其Postman HTTP Post请求代码。

POST /upload.php HTTP/1.1
Host: 12.99.22.12
Cache-Control: no-cache
Postman-Token: 79b6cf45-9180-70e1-2136-8af2f8f67271
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="dosya"; filename="deneme--1.csv"
Content-Type: application/vnd.ms-excel


            ------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="yukle"

Yükle
------WebKitFormBoundary7MA4YWxkTrZu0gW--

我写了这段代码。它是否相同?

 $postfields = array("dosya" => "@$path","yukle"=>"Yükle","filename"=>"deneme--1.csv");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
        //curl_setopt($ch, CURLOPT_HEADER , 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $sonuc=curl_exec($ch);
        curl_close($ch);

文件没有上传。为什么? Http代码工作但PHP代码不起作用。我不知道为什么。我的错在哪里?

1 个答案:

答案 0 :(得分:0)

您需要创建要上传的文件

    // php version >= 5.5
if (function_exists('curl_file_create')) { 
    $file_post = curl_file_create($full_path_file);
} else { 
    $file_post = '@' . realpath($full_path_file);
}
$postfields = array("dosya" => $file_post,"yukle"=>"Yükle","filename"=>"filename");