【问题标题】:Sending raw multipart data through jquery $.post() and php通过 jquery $.post() 和 php 发送原始多部分数据
【发布时间】:2011-05-21 08:16:45
【问题描述】:

我需要使用 php POST 发送原始多部分数据,但没有 html 表单...我使用 jquery $.post() 开始该过程(目的是更改 Twitter 帐户的背景)。

我怎样才能做到这一点?这是我当前(但仍然不完整)的代码:

1) 图像文件名被插入到这个隐藏的输入字段中:

<input type="hidden" id="profile_background_image_url" value="oats.jpg" />

2) 当点击提交按钮时,一个javascript函数被触发......它调用:

$.post('helper.php',{
 profile_background_image_url:$('#profile_background_image_url').val()
});

3) helper.php

$param = array();
$param['image'] = '/www/uploads/'.$_POST['profile_use_background_image'];
$status = $connection->post('account/update_profile_background_image',$param);

注意事项

  1. 所有后台文件都在 /www/uploads 本地目录中。
  2. 我正在使用 Abraham Williams 的 twitteroauth 库 0.2

底线,在第三步中,我需要将 原始多部分数据 中的 $param['image'] 发送到 $connection 对象(推特库)。

有什么想法吗?

部分参考:http://dev.twitter.com/doc/post/account/update_profile_background_image

【问题讨论】:

  • file_get_contents() 工作吗?

标签: php file-upload jquery curl multipart


【解决方案1】:

是的,我现在看到他将帖子字段数组构建为查询字符串,这意味着您必须手动设置内容类型,并且 image 字段中的 @ 键不会发挥它的魔力,因为它只适用于一个数组参数。更重要的是,我看不到在不修改库或扩展它并替换某些功能的情况下修改标题的方法。


我会尝试将@ 添加到image 参数的文件路径中,例如:

$param['image'] = '@/www/uploads/'.$_POST['profile_use_background_image'];

这是使用 cURL 的便捷方式,看起来 libray 基本上使用 cURL 来发出请求,所以应该可以。

【讨论】:

    【解决方案2】:

    解决了!

    curl_setopt($ci, CURLOPT_POST, TRUE);
            if(is_array($files)){
              $post_file_array = array();
              foreach($files as $key=>$value){
                 $post_file_array[$key] = "@{$value}";
              }
              curl_setopt($ci, CURLOPT_POSTFIELDS, $post_file_array);
              if (!empty($postfields)) {
                $url = "{$url}?{$postfields}";
              }
            }
            else if (!empty($postfields)) {
              curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
    

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多