1
I have an api in Slim Framework, within the "Service A" of the api I am using a Curl to call the "Service B" of the api, this "Service B" downloads images by their url using another Curl, it runs the "Service A" and calls the "Service B", it upsert the "Service B" successfully and upsert the "Service A" successfully, only that the image comes variously, it seems that the "Service B" URL does not save the image even by upsert in the database.
Below is an example of the code:
"Service A":
$app->post('/upsertTeste', function () use ($app, $objReturn) {
$picture_url = = urldecode($app->request->post('picture'));
$url = "http://servico.com/table2/salvarImg";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "picture=".$picture_url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$data = curl_exec($ch);
$return = json_decode($data, TRUE);
$foto = Model1::updateOrCreate(["id" => $id], ["img" => $return["img"]]);
});
"Service B":
$app->post('/salvarImg', function () use ($app, $objReturn) {
$picture_url = = urldecode($app->request->post('picture'));
$path = DIR_UPLOAD;
$uniqid = uniqid().".jpg";
$savefile = download_image($picture_url, $uniqid, $path);
$foto = Model2::updateOrCreate(["id" => $id], ["img" => $uniqid]);
});
//função pega no stackoverflow em outro post
function download_image($url,$filename,$path){
if(file_exists($filename)){
@unlink($filename);
}
$fp = fopen($filename,'w');
if($fp){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$result = parse_url($url);
curl_setopt($ch, CURLOPT_REFERER, $result['scheme'].'://'.$result['host']);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
$raw=curl_exec($ch);
curl_close ($ch);
if($raw){
fwrite($fp, $raw);
}
fclose($fp);
rename($filename, $path.$filename);
if(!$raw){
@unlink($filename);
return false;
}
return true;
}
return false;
}
If I just run the "B Service" it saves the image successfully instead of seeing a picture.