php - Pass Laravel Image Object To External API with CURL? -
at moment have api working along dummy test site:
dummy site = http://www.example.com api = http://api.myapi.com
at moment api works though i'm unable ajax image upload work. here how api working in instance:
send ajax request http://www.example.com/data/image_handler check validation , access in http://www.example.com/data/image_handler send curl request$input http://api.myapi.com/data/image_handler http://api.myapi.com/data/image_handler passes info mediafile::createmediafile the step failing i'm sending info http://www.example.com/data/image_handler > http://api.myapi.com/data/image_handler. i'm trying send input::file() object.
js ajax request - works fine:
var info = new formdata(); if ($this.parents('.media-data') && $this.parents('.media-data').data('media') > 0) { data.append('mediaid', $this.parents('.media-data').data('media')); } data.append('file', input); data.append('medialink', 'media_link'); data.append('objectid', 1); data.append('objecttype', 1); data.append('usagetype', 1); data.append('mediatype', 1); $.ajax({ async: false, url: '/data/image_handler', type: 'post', data: data, cache: false, datatype: 'json', processdata: false, // don't process files contenttype: false, // set content type false jquery tell server query string request success: function(data, textstatus, jqxhr) { response = data; homecoming response; }, error: function(jqxhr, textstatus, errorthrown) { response = data; homecoming response; } }); http://www.example.com/data/image_handler
public function imagehandler(){ try{ $input = input::all(); echo '<pre>'; print_r($input); die(); $input['class'] = 'mediafile'; if( isset($input['mediaid']) && $input['mediaid'] && $input['mediaid'] != 0 ){ $input['function'] = 'updatemediafile'; }else{ $input['function'] = 'createmediafile'; } $class = $input['class']; $function = $input['function']; if( count(config::get('ajax.'.$input['class'].'.'.$input['function'].'.validation_rules')) ) { $validator = validator::make($input, config::get('ajax.'.$input['class'].'.'.$input['function'].'.validation_rules')); if ($validator->fails()) { $data = array( 'response' => 'failed', 'data' => array( 'error_messages' => array( $validator->errors()->toarray(), ), ), ); homecoming response::json($data); } } $token = new token(); $token->token = str_random(32); $token->save(); $input['token'] = $token->token; $input['site_id'] = config::get('api_setup.site_id'); $input['application_id'] = config::get('api_setup.application_id'); $input['key'] = config::get('api_setup.key'); $ch = curl_init(); curl_setopt($ch, curlopt_url, config::get('api_setup.api_image_url').http_build_query($input) ); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_connecttimeout, 5); $data = curl_exec($ch); curl_close($ch); echo '<pre>'; print_r($data); die(); if(is_object($data)) { $data = array( 'response' => 'success', 'data' => $data->toarray(), ); } else { $data = array( 'response' => 'failed', 'data' => array( 'error_messages' => array( 'here' => 'here', 'something_went_wrong' => lang::get('ajax.errors.wrong_file_format'), ), ), ); } homecoming response::json($data); } catch( exception $e) { homecoming $e->getmessage(); $data = array( 'response' => 'failed', 'data' => array( 'error_messages' => array( 'or_here' => 'or_here', 'something_went_wrong' => lang::get('ajax.errors.something_went_wrong'), ), ), ); homecoming response::json($data); } } http://api.myapi.com/data/image_handler
public function imagehandler(){ try{ $input = input::all(); echo '<pre>'; print_r($input); die(); $class = $input['class']; $function = $input['function']; $functionparams = array(); $reference = new reflectionmethod($class, $function); foreach( $reference->getparameters() $param) { if( isset($input[$param->name]) ) { $functionparams[$param->name] = $input[$param->name]; } else { $functionparams[$param->name] = config::get('ajax.'.$class.'.'.$function.'.function_parameters_defaults.'.$param->name); } } // $reflectionmethod = new reflectionmethod($class, $function); $data = $reference->invokeargs(new $class(), $functionparams); if(is_object($data)) { $data = array( 'response' => 'success', 'data' => $data->toarray(), ); } else { $data = array( 'response' => 'failed', 'data' => array( 'error_messages' => array( 'something_went_wrong' => lang::get('ajax.errors.wrong_file_format'), ), ), ); } homecoming response::json($data); } catch( exception $e) { homecoming $e->getmessage(); $data = array( 'response' => 'failed', 'data' => array( 'error_messages' => array( 'something_went_wrong' => lang::get('ajax.errors.something_went_wrong'), ), ), ); homecoming response::json($data); } } here response looks when print_r($input) @ first controller - http://www.example.com/data/image_handler:
array ( [medialink] => media_link [objectid] => 1 [objecttype] => 1 [usagetype] => 1 [mediatype] => 1 [file] => symfony\component\httpfoundation\file\uploadedfile object ( [test:symfony\component\httpfoundation\file\uploadedfile:private] => [originalname:symfony\component\httpfoundation\file\uploadedfile:private] => italian-guido-gangsta-italian-guido-italy-gangsta-1291824813.jpg [mimetype:symfony\component\httpfoundation\file\uploadedfile:private] => image/jpeg [size:symfony\component\httpfoundation\file\uploadedfile:private] => 74294 [error:symfony\component\httpfoundation\file\uploadedfile:private] => 0 [pathname:splfileinfo:private] => /applications/mamp/tmp/php/phpdxmwcc [filename:splfileinfo:private] => phpdxmwcc ) ) here response looks when print_r($input) @ sec controller - http://api.myapi.com/data/image_handler - here can see $input['file'] doesn't exist here because object believe.
array ( [medialink] => media_link [objectid] => 1 [objecttype] => 1 [usagetype] => 1 [mediatype] => 1 [class] => mediafile [function] => createmediafile [token] => fixi0cv4la4hwixmtevhoxng8qkotx9s [site_id] => 2 [application_id] => yazk0hfdauwjspa70s8r [key] => 25quv4immjhkmh3esqzf ) php api curl file-upload laravel
No comments:
Post a Comment