当我尝试从Google获取图片时,对于单个搜索,它会提供4张图片,但由于图片大小的原因,我无法获取一些图片,因此出现以下错误:
"file_get_contents(): Failed to enable crypto"
有什么解决办法吗?这里是我的代码:
"file_get_contents(): Failed to enable crypto"
有什么解决办法吗?这里是我的代码:
<?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;
?>