当前位置: 首页 > 面试题库 >

使用cURL在PHP中发布文件字符串?

令狐宏伟
2023-03-14
问题内容

我想知道是否有可能在文件只是字符串的情况下发布文件以及其他表单数据?

我知道您可以通过在文件路径前面加上“ @”前缀来发布文件系统中已经存在的文件。

但是,我想绕过创建一个临时文件,而仅将该文件作为字符串发送,但是我不确定如何在PHP中使用cURL构造请求。

干杯

    $postFields = array(
        'otherFields'   => 'Yes'
        ,'filename'     => 'my_file.csv'
        ,'data'         => 'comma seperated content'
    );

    $options = array(
        CURLOPT_RETURNTRANSFER  => true
        ,CURLOPT_SSL_VERIFYPEER => false
        ,CURLOPT_SSL_VERIFYHOST => 1
        ,CURLOPT_POSTFIELDS     => $postFields
        ,CURLOPT_HTTPHEADER     => array(
            'Content-type: multipart/form-data'
        )
    );

问题答案:

应该可以:这是通过浏览器发布的表格(忽略了不相关的字段):

POST http://host.example.com/somewhere HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7da16b2e4026c
Content-Length: 105732

-----------------------------7da16b2e4026c
Content-Disposition: form-data; name="NewFile"; filename="test.jpg"
Content-Type: image/jpeg

(...raw JPEG data here...)
-----------------------------7da16b2e4026c
Content-Disposition: form-data; name="otherformfield"

content of otherformfield is this text
-----------------------------7da16b2e4026c--

因此,如果我们自己构建POST主体并设置一个或多个额外的标头,则我们应该能够模拟此:

// form field separator
$delimiter = '-------------' . uniqid();
// file upload fields: name => array(type=>'mime/type',content=>'raw data')
$fileFields = array(
    'file1' => array(
        'type' => 'text/plain',
        'content' => '...your raw file content goes here...'
    ), /* ... */
);
// all other fields (not file upload): name => value
$postFields = array(
    'otherformfield'   => 'content of otherformfield is this text',
    /* ... */
);

$data = '';

// populate normal fields first (simpler)
foreach ($postFields as $name => $content) {
   $data .= "--" . $delimiter . "\r\n";
    $data .= 'Content-Disposition: form-data; name="' . $name . '"';
    // note: double endline
    $data .= "\r\n\r\n";
}
// populate file fields
foreach ($fileFields as $name => $file) {
    $data .= "--" . $delimiter . "\r\n";
    // "filename" attribute is not essential; server-side scripts may use it
    $data .= 'Content-Disposition: form-data; name="' . $name . '";' .
             ' filename="' . $name . '"' . "\r\n";
    // this is, again, informative only; good practice to include though
    $data .= 'Content-Type: ' . $file['type'] . "\r\n";
    // this endline must be here to indicate end of headers
    $data .= "\r\n";
    // the file itself (note: there's no encoding of any kind)
    $data .= $file['content'] . "\r\n";
}
// last delimiter
$data .= "--" . $delimiter . "--\r\n";

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_HTTPHEADER , array(
    'Content-Type: multipart/form-data; boundary=' . $delimiter,
    'Content-Length: ' . strlen($data)));  
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);

这样,我们正在做所有繁重的工作,并相信cURL不会破坏它。



 类似资料:
  • 我正在尝试使用curl向REST服务发布一个xml文件(utf-16编码)。REST服务需要“multipart/form-data”内容类型。 Curl脚本:Curl-k-i-h“content-type=multipart/form-data”-f“filename=@file.xml;type=text/xml”-x POST-u: 然而,我在运行脚本时得到500个内部服务器错误。 响应:<

  • 问题内容: 我正在存储Facebook用户ID和访问令牌。我可以将此信息发布到所选用户的留言墙上吗?在这里可以找到以下代码:http : //developers.facebook.com/docs/reference/api/post/ 我只是不确定如何用php运行它。 问题答案:

  • 问题内容: 我可能还差得远,但是我整个下午一直在尝试在本教程PHP框架教程中运行curlpost命令。我不明白的是PHP应该如何解释我的POST,它总是以空数组的形式出现。 (其中的斜线只是让我看起来不像是个白痴,而是我使用PHP 5.2在Windows上执行此操作,也曾在与Linux curl相同版本的Linux服务器上尝试过) 我必须缺少一些东西,因为它看起来很简单,帖子解释得不好,如果可以的

  • 输出为 所以,即使在我自己的服务器上,我也没有得到正确的数据,它是空数组。我想通过http://docs.shopify.com/api/customer#create使用json实现REST

  • 我试图用java spring创建一个接受csv文件的restendpoint。 我的控制器如下所示: 将-vv选项添加到curl调用中会导致以下输出:

  • 问题内容: 我无法通过CURL将表单数据发布到位于不同主机上的接收PHP脚本。 我得到一个错误 这是我要发布的数组: 这是发生错误的行: 第三个参数 必须 是一个数组,因为在通过同一数组发送文件时,我需要将标头设置为该数组,因此无法将数组转换为查询字符串或使用。 另外,我无法访问接收主机上的代码,因此无法序列化和反序列化数组。 我假设 名称 键的值是数组是导致此错误的原因,我还假设它不支持多维数组