Skip Laravel Image Object for external API with CURL?
I currently have an API that works alongside a dummy test site:
Dummy Site = http://www.example.com API = http://api.myapi.com
The API is working at the moment, although I cannot get the ajax image loading to work. This is how my API works on this instance:
- Send an AJAX request http://www.example.com/data/image_handler
- Check correctness and access at http://www.example.com/data/image_handler
- Submit a CURL request with everyone
$input
at http://api.myapi.com/data/image_handler - http://api.myapi.com/data/image_handler transfers data to
MediaFile::createMediaFile
The failed step is sending data from http://www.example.com/data/image_handler > http://api.myapi.com/data/image_handler . I am trying to send Input :: file () as an object.
JS AJAX request - works great:
var data = 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 the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR) {
response = data;
return response;
},
error: function(jqXHR, textStatus, errorThrown) {
response = data;
return 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(),
),
),
);
return 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'),
),
),
);
}
return Response::json($data);
} catch( Exception $e) {
return $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'),
),
),
);
return 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() as $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'),
),
),
);
}
return Response::json($data);
} catch( Exception $e) {
return $e->getMessage();
$data = array(
'response' => 'failed',
'data' => array(
'error_messages' => array(
'something_went_wrong' => Lang::get('ajax.errors.something_went_wrong'),
),
),
);
return Response::json($data);
}
}
This is what the response looks like when you first run print_r ($ input) - 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
)
)
This is what the response looks like when I print_r ($ input) on the second controller - http://api.myapi.com/data/image_handler - here you can see that $input['file']
it doesn't exist here because it is an object that I 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
)
source to share
No one has answered this question yet
Check out similar questions: