代码之家  ›  专栏  ›  技术社区  ›  Franz

这是在CakePhp3中创建和发布多部分表单数据的正确方法吗?

  •  0
  • Franz  · 技术社区  · 6 年前

    CakePhp3中的多部件表单数据 . 表单应该包含一些关于用户的信息(文本字段)和图像。

    我在做这样的事情:

    $request = [
      'fistname'=>$user->firstname,
      'lastname'=>'$user->lastname',
      '_session'=>'$session'
    ];
    
    $form = new FormData();
    foreach ($request as $key => $value) {
      $form->add($key,$request);
    }
    $file = $form->addFile('upload',fopen(WWW_ROOT.'img/picture.png','r'));
    $file->contentId('mypicture'); // <-- not sure what this is
    $file->disposition('attachment');
    
    $response = $http->post(
      $url,
      (string)$form,
      ['headers' => ['Content-Type' => $form->contentType()]]
    );
    

    谢谢你的帮助。

    object(Cake\Http\ServerRequest) {
    trustProxy => false
    ...
    [protected] data => [
        'fistname' => [
            'fistname' => 'Test',
            'lastname' => 'Test'
        ],
        'lastname' => [
            'fistname' => 'Test',
            'lastname' => 'Test'
        ],
        'upload' => [
            'tmp_name' => '/private/var/folders/g5/jjd1vc557bs21hq805lcxjk80000gn/T/phpAVXNqK',
            'error' => (int) 0,
            'name' => 'SELL5BAE2B6348272_gallery_1.png',
            'type' => 'image/png',
            'size' => (int) 200231
        ]
    ]
    ...
    

    如果我发布到API服务器,它就不工作了。 我对卷曲也做了同样的处理,效果很好:

    $filename = WWW_ROOT.'ufiles/medium/'.$img['image_1'];
    $cFile = curl_file_create($filename);
    $request = [
      '_operation'=>'uploadFile',
      'id'=>$ticket_id,
      '_session'=>$session,
      'file' => $cFile
    ];
    
    try{
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_URL,$this->crm['endpoint']);
      curl_setopt($ch, CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
      $response=curl_exec($ch);
      curl_close ($ch);
    }catch(\Exception $e){
      $response = null;
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Marijan    6 年前

    反问:有效吗?

    如你所见, $file->contentId()
    https://github.com/cakephp/cakephp/blob/master/src/Http/Client/FormDataPart.php#L114

    我不确定这一点,但我不认为你必须设置它,因为它似乎没有必要的 multipart/form-data :
    https://www.w3.org/TR/2006/REC-xforms-20060314/slice11.html#serialize-form-data


    编辑
    我做了一些测试:它。作品。以下是我尝试的:

    use Cake\Http\Client;
    use Cake\Http\Client\FormData;
    
    public function test() {
    
        $request = [
            'fistname'=>'Test',
            'lastname'=>'Test'
        ];
    
        $form = new FormData();
        $http = new Client();
    
        foreach ($request as $key => $value) {
            $form->add($key,$request);
        }
        $file = $form->addFile('upload',fopen(WWW_ROOT.'img/awards.png','r'));
        $file->contentId('mypicture'); // <-- not sure what this is
        $file->disposition('attachment');
    
        $response = $http->post(
          'http://www.testurl.dev/test',
          (string)$form,
          ['headers' => ['Content-Type' => $form->contentType()]]
        );
        var_dump($response);
        exit;
    }
    

    的输出 var_dump($response) :

    object(Cake\Http\Client\Response)#150 (12) {
      ["code":protected]=>int(200)
      …
      ["headers":protected]=>array(12) {
        ["Date"]=>array(1) {
          [0]=>string(29) "Fri, 28 Sep 2018 12:44:31 GMT"
        }
        …
      }
    }
    

    [28/Sep/2018:14:44:31 +0200] "POST /test HTTP/1.1" 200 6852 "-" "CakePHP"