codeigniter - Curl not posting file in PHP -
i using brightcove api upload video files server brightcove account. had working following code:
$fields = array( 'json' => json_encode( array( 'method' => "create_video", 'params' => array( 'video' => array( 'name' => $video->submission_by_name.' '.time(), 'shortdescription' => $video->submission_question_1 ), "token" => $this->config->item('brightcove_write_token'), "encode_to" => "mp4", "create_multiple_renditions" => "true" )) ), 'file' => new curlfile(fcpath.'assets/uploads/submitted/'.$video->filename) ); //open connection $ch = curl_init(); curl_setopt($ch,curlopt_url, $this->config->item('brightcove_write_endpoint')); curl_setopt($ch,curlopt_post, count($fields)); curl_setopt($ch,curlopt_postfields, $fields); curl_setopt($ch, curlopt_returntransfer, true); //execute post $result = json_decode(curl_exec($ch));
this working locally live server running php 5.3 can't use new curlfile()
how achieve sending file in way work php 5.3.3? tried changing file filed use @
syntax instead so:
'file' => '@' . fcpath.'assets/uploads/submitted/'.$video->filename
but doesn't seem work, api returns error saying:
filestreamrequirederror: upload requires multipart/form-data post valid filestream
so looks file isn't being posted through.
i've tried copying curl_file_create
function so:
private function custom_curl_file_create($filename, $mimetype = '', $postname = '') { if($mimetype=='') { $mimetype = mime_content_type($filename); } return "@$filename;filename=" . ($postname ?: basename($filename)) . ($mimetype ? ";type=$mimetype" : ''); }
and doing:
'file' => $this->custom_curl_file_create(fcpath.'assets/uploads/submitted/'.$video->filename)
this doesn't work either though, api returns same error before
there "@" issue on multipart post requests apparently. found following information solved problem:
solution php 5.5 or later: - enable curlopt_safe_upload. - use curlfile instead of "@". solution php 5.4 or earlier: - build multipart content body youself. - change "content-type" header yourself. following snippet :d <?php /** * safe multipart post request php5.3 ~ php 5.4. * * @param resource $ch curl resource * @param array $assoc "name => value" * @param array $files "name => path" * @return bool */ function curl_custom_postfields($ch, array $assoc = array(), array $files = array()) { // invalid characters "name" , "filename" static $disallow = array("\0", "\"", "\r", "\n"); // build normal parameters foreach ($assoc $k => $v) { $k = str_replace($disallow, "_", $k); $body[] = implode("\r\n", array( "content-disposition: form-data; name=\"{$k}\"", "", filter_var($v), )); } // build file parameters foreach ($files $k => $v) { switch (true) { case false === $v = realpath(filter_var($v)): case !is_file($v): case !is_readable($v): continue; // or return false, throw new invalidargumentexception } $data = file_get_contents($v); $v = call_user_func("end", explode(directory_separator, $v)); $k = str_replace($disallow, "_", $k); $v = str_replace($disallow, "_", $v); $body[] = implode("\r\n", array( "content-disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"", "content-type: application/octet-stream", "", $data, )); } // generate safe boundary { $boundary = "---------------------" . md5(mt_rand() . microtime()); } while (preg_grep("/{$boundary}/", $body)); // add boundary each parameters array_walk($body, function (&$part) use ($boundary) { $part = "--{$boundary}\r\n{$part}"; }); // add final boundary $body[] = "--{$boundary}--"; $body[] = ""; // set options return @curl_setopt_array($ch, array( curlopt_post => true, curlopt_postfields => implode("\r\n", $body), curlopt_httpheader => array( "expect: 100-continue", "content-type: multipart/form-data; boundary={$boundary}", // change content-type ), )); }
this retrieved user contributed notes section of php curlfile::__construct documentation
i used , worked:
$fields = array( 'json' => json_encode( array( 'method' => "create_video", 'params' => array( 'video' => array( 'name' => $video->submission_by_name.' '.time(), 'shortdescription' => $video->submission_question_1 ), "token" => $this->config->item('brightcove_write_token'), "encode_to" => "mp4", "create_multiple_renditions" => "true" )) ) ); $files = array( 'file' => fcpath.'assets/uploads/submitted/'.$video->filename ); //open connection $ch = curl_init(); curl_setopt($ch,curlopt_url, $this->config->item('brightcove_write_endpoint')); $this->curl_custom_postfields($ch, $fields, $files); curl_setopt($ch, curlopt_returntransfer, true); //execute post $result = json_decode(curl_exec($ch));
Comments
Post a Comment