curl within another curl to save images

1

I have an api in Slim Framework, inside the "Service A" api I am using a curl to call "Service B" api, this "Service B" downloads images through the url of them using another curl, he runs "Service A" and calls "Service B", he successfully upsets "Service B" and successfully upsets "Service A", except that the image is empty, "Service B" does not save the image even by doing the upsert in the bank.

Here 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 run only "Service B" it saves the image successfully instead of a straight image.

    
asked by anonymous 24.09.2018 / 20:24

0 answers