【问题标题】:post json data with php curl for multipart/form-data for file upload vía cakephp 2使用 php curl 为 multipart/form-data 发布 json 数据以通过 cakephp 2 上传文件
【发布时间】:2013-04-04 18:48:05
【问题描述】:

我想使用 php curl 为带有文件上传字段的 multipart/form-data 发布 json 数据。 我在 cakephp 2 中尝试了这个操作:

public function json_post(){

    $this->autoRender = false;
    debug('json post test');

    $data = array('Post' => array(
        'subject' => 'test subject content',
        'body' => 'test body content'
        'fileName' => '/Users/mar/Pictures/cow_wall_90.jpg'
    ));                                                                    
    $data_string = json_encode($data);                                                                                   

    $ch = curl_init('http://www.mydomain.com/posts/phone_upload.json');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            //'Content-Type: multipart/form-data', 
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   

    $result = curl_exec($ch);

    debug($result); 


}

fileName 相当于表单中的文件上传字段。 主题和正文等同于表单中的文本字段。 我错过了一些可能在数据数组或 Content-Type 中但找不到问题的东西。

感谢您的帮助,问候,马丁

【问题讨论】:

    标签: json rest cakephp curl multipartform-data


    【解决方案1】:

    来自http://dtbaker.net/web-development/uploading-a-file-using-curl-in-php/注意文件路径前面的@,这是神奇的部分。

    <?php
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
        curl_setopt($ch, CURLOPT_POST, true);
        // same as <input type="file" name="file_box">
        $post = array(
            "file_box"=>"@/path/to/myfile.jpg",
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
        $response = curl_exec($ch);
    ?>
    

    看来你错过了魔法部分。

    您也可以从 cURL 切换到 CakePHP 的 HttpSocket,但这只是个人偏好。 http://book.cakephp.org/2.0/en/core-utility-libraries/httpsocket.html

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2011-04-22
    相关资源
    最近更新 更多