File_get_contents (): Failed to enable cryptography

When I try to get images from Google and it gives 4 images for one search, I can’t get some images due to image size. so i get an error like

"file_get_contents (): Failed to enable cryptography

Is there any solution? Here is my code

    <?php
    use frontend\models\Giftinterests;
    use yii\amazonproductapi\AmazonProductAPI;
    use yii\helpers\Url;
    use yii\web\NotFoundHttpException;
    use yii\image\ImageDriver;

    $sql_1 = 'SELECT * FROM `tn_gift_interests` where `gift_interest_status`="1" order by `gift_interest_id` ASC';
    $Query_Gift = Giftinterests::findBySql($sql_1)->all();

    $sql_2="TRUNCATE `tn_gift_google_image`";
    $query_1 = Yii::$app->db->createCommand($sql_2)->execute();




    function get_url_contents($url) {
        $crl = curl_init();

       curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
        curl_setopt($crl, CURLOPT_URL, $url);
        curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($crl, CURLOPT_SSLVERSION,3);
        curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, true);

        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
    }

    $width=260;$height=229;
    $a=0;
    foreach($Query_Gift as $two):
    $a++;
    $amazon = new AmazonProductAPI('xxxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxx', 'xx','xx');

           $similar = array(
            'Operation' => 'BrowseNodeLookup',
            'BrowseNodeId' => $two->gift_interest_value
          );

        $result = $amazon->queryAmazon($similar);
        $result = json_decode(json_encode($result));
        $b=0;

    $json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="'.str_replace(' ','',$two->gift_interest_name).'"');
    $data = json_decode($json);


    foreach ($data->responseData->results as $result_1) {

        $results[] =$result_1->url;             
    }

    $c=$a * 4;



    foreach ($result->BrowseNodes->BrowseNode->Children->BrowseNode as $one){
            $b++;

                $Id_Node=$one->BrowseNodeId;

                if($b==1){
                $name=$results[($c-4)];
                }
                else{
                    $name=$results[($c-2)];
                    $b=0;
                }
                    $filename   = pathinfo($name, PATHINFO_FILENAME).'_'.$one->BrowseNodeId;
                    $ext        = pathinfo($name, PATHINFO_EXTENSION);
                    $newName        =preg_replace( "/[^a-zA-Z0-9\._]+/", "-", strtolower($filename) ).'.'.$ext;

                    $ImgSave='my/path/' .$newName;
                    file_put_contents($ImgSave, file_get_contents($name));
                 $re200='/my/another_path/' .$newName;
                     $file_2=$ImgSave; 
                     $image_2=Yii::$app->image->load($file_2);
                     $Img_2= $image_2->resize($width,$height, 'NONE')->render();
                     $image_2->save($re200, 100);

                    $Img='assets2/images/category/' .$newName;
        }
        endforeach;
   ?>
+4
source share
2 answers

There seems to be a problem with OpenSSL and SSL 3.

See here for a solution: OPENSSL file_get_contents (): Failed to enable cryptography

0
source
curl_setopt($crl, CURLOPT_SSLVERSION,3);

Do not do this. SSLv3 is deprecated and is no longer supported by most web servers (including Google).

0
source

Source: https://habr.com/ru/post/1613939/


All Articles