当我尝试从谷歌获取图像并且它为单次搜索提供4张图像时,由于图像尺寸,我无法获得一些图像。所以我得到像
这样的错误" file_get_contents():无法启用加密"
有什么解决方案吗? 这是我的代码
<?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;
?>
答案 0 :(得分:1)
curl_setopt($crl, CURLOPT_SSLVERSION,3);
不要那样做。 SSLv3已被弃用,大多数网络服务器(包括Google)不再支持SSLv3。
答案 1 :(得分:0)
OpenSSL和SSL 3似乎存在问题。
请点击此处查看解决方案:OPENSSL file_get_contents(): Failed to enable crypto