Help us understand the problem. What is going on with this article?

FuelPHP Request_Curlでファイルアップロードをする

More than 3 years have passed since last update.

はじめに

FuelPHP標準のRequest_Curlでファイルアップロードをする機会があったのでそのサンプルになります。他でやればいいのですが、標準使いたかったので調べました。
Curlの場合@を使用すればファイルアップロードになるはずなのですができなかったので微妙にはまりました。

環境
FuelPHP 1.7
PHP 5.6.18

概要

  1. ソース作成
  2. 実行

詳細


1. ソース作成

ソースを作成します。

namespace Fuel\Tasks;

class UploadTest {

    public function run() {
        $params = array(
            'post_param1'   => 'データ1'
        ,   'fileupload'    => \CURLFile(dirname(__FILE__)."/upload.csv");
        );
        $postparam  = array(
            'form-data'     => $params
        );

        $curl       = \Request::forge($url, 'curl');
        $curl->set_method('post');
        $curl->set_params($postparam);
        $curl->set_option(CURLOPT_RETURNTRANSFER,true); 
        $curl->set_option(CURLOPT_BINARYTRANSFER,true);
        $curl->set_header('Content-Type','multipart/form-data');
        $execResult = $curl->execute();
        $response   = $execResult->response();
        $result     = $response->body;
        print_r($result);
    }
}

2. ソース作成

実行します。

$oil refine UploadTest

あとがき

set_optionでCURLOPT_RETURNTRANSFERはソースを見るとCURLOPT_つけている感じなので文字列で指定する場合はRETURNTRANSFERでも良いかもしれません。

curl.php

    public function set_options(array $options)
    {
        foreach ($options as $key => $val)
        {
            if (is_string($key) && ! is_numeric($key))
            {
                $key = constant('CURLOPT_' . strtoupper($key));
            }

            $this->options[$key] = $val;
        }

        return $this;
    }

その他

参考
PHPでcURLのクソ仕様 "@" を回避する

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away