đăng dữ liệu json với php curl cho đa phần / dữ liệu biểu mẫu để tải lên tệp nếu cakephp 2 (post json data with php curl for multipart/form-data for file upload vía cakephp 2)


問題描述

đăng dữ liệu json với php curl cho đa phần / dữ liệu biểu mẫu để tải lên tệp nếu cakephp 2 (post json data with php curl for multipart/form‑data for file upload vía cakephp 2)

i want to post json data with php curl for a multipart/form‑data with a file upload field. i tried this in cakephp 2 in a action:

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 is the equivalent for the file upload field in a form. subject and body are the equivalent for text fields in a form. i miss something may be in the data array or in the Content‑Type but can't find the problem.

thanks for any help, regards, martin


參考解法

方法 1:

From http://dtbaker.net/web‑development/uploading‑a‑file‑using‑curl‑in‑php/: notice the @ infront of the file path, this is the magic part.

<?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);
?>

Seems you were missing the magic part.

You may also switch from cURL to CakePHP's HttpSocket, but that's just personal prefrence. http://book.cakephp.org/2.0/en/core‑utility‑libraries/httpsocket.html

(by martjohhniedoe)

參考文件

  1. post json data with php curl for multipart/form‑data for file upload vía cakephp 2 (CC BY‑SA 3.0/4.0)

#curl #JSON #REST #multipartform-data #cakephp






相關問題

如何在所有 PayPal 交易中退還費用 (How to return fee in all PayPal transactions)

Cú pháp lệnh curl để đăng ký device_token trên Urban Airship là gì? (What is curl command syntax for device_token registration on Urban Airship?)

Nhận thời lượng video trên youtube từ URL với bash (Get youtube video duration from URL with bash)

đăng dữ liệu json với php curl cho đa phần / dữ liệu biểu mẫu để tải lên tệp nếu cakephp 2 (post json data with php curl for multipart/form-data for file upload vía cakephp 2)

ApacheMonitor 上的捲曲錯誤 (Curl error on ApacheMonitor)

從 ASP.net 使用 PHP 中的 POST 數據抓取數據 (Scraping data with POST data in PHP from ASP.net)

收聽推送消息? (Listen to a push message?)

在 PHP 中使用 cURL 的 RAW POST (RAW POST using cURL in PHP)

使用 php 和 curl 更新 mediawiki (using php and curl to update mediawiki)

在 Makefile 中鏈接 cURL (Linking cURL in Makefile)

將我的 .php 輸出發送到 curl 命令,該命令使用機器人在電報上向我發送消息 (Send my .php output to a curl command that messages me on telegram using a bot)

使用 Google My Business API 和 PHP 更新/修補本地帖子 (Updating/Patching a Local Post with Google My Business API and PHP)







留言討論