首页 文章

通过 jquery $.post()和 php 发送原始多部分数据

提问于
浏览
0

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

我怎样才能实现这一目标?这是我目前(但仍然不完整)的代码:

  1. 图像文件名插入此隐藏的输入字段中:
<input type="hidden" id="profile_background_image_url" value="oats.jpg" />
  1. 当点击提交按钮时,会触发一个 javascript 函数...并调用:
$.post('helper.php',{
 profile_background_image_url:$('#profile_background_image_url').val()
});
  1. helper.php
$param = array();
$param['image'] = '/www/uploads/'.$_POST['profile_use_background_image'];
$status = $connection->post('account/update_profile_background_image',$param);

注意事项

  • 所有后台文件都在/www/uploads 本地目录中。

  • 我正在使用亚伯拉罕·威廉姆斯的 twitteroauth 图书馆 0.2

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

有任何想法吗?

一些参考文献:http://dev.twitter.com/doc/post/account/update_profile_background_image

2 回答

  • 0

    解决了!

    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);
            }
    
  • 1

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


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

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

    这是使用 cURL 执行此操作的便捷方式,看起来 libray 基本上使用 cURL 来发出请求,因此应该可以正常工作。

相关问题