我在尝试将动态变量应用于 URL 时遇到错误,它本身也被设置为变量,$sURL。
例如,如果我将".$prNameUrl."更改为关键字 CATS,它可以正常工作。但是一旦我输入一个变量,我就会收到错误:注意:尝试获取非对象的属性
$prNameUrl = $v->product_name.' '.$v->product_category.' '.$v->product_format;
$prNameUrlTwo = 'https://api.cognitive.microsoft.com/bing/v7.0/images/search?q='.$prNameUrl.'&count=4&mkt=en-US';
$sURL = $prNameUrlTwo;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data',
'Ocp-Apim-Subscription-Key: xxxxxxxxx'
));
$contents = curl_exec($ch);
$myContents = json_decode($contents);
if(count($myContents->value) > 0) {
foreach ($myContents->value as $imageContent) {
echo "<a href='{$imageContent->webSearchUrl}'><img src='{$imageContent->thumbnailUrl}' width='290' height='200' /></a>";
}
}
我已经解决了"urlencode"的问题。
$sURL = $WebSearchURL . urlencode( ''' . $prNameUrl . ''') . '&count=4&mkt=en-US';
带解决方案
的完整代码
$prNameUrl = $v->product_name.' '.$v->product_category.' '.$v->product_format;
echo $prNameUrl;
echo '<br>';
echo '<br>';
$prNameUrlTwo = 'https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=';
$WebSearchURL = $prNameUrlTwo ;
$sURL = $WebSearchURL . urlencode( ''' . $prNameUrl . ''') . '&count=4&mkt=en-US';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data',
'Ocp-Apim-Subscription-Key: xxxxxxxxxx'
));
$contents = curl_exec($ch);
$myContents = json_decode($contents);
if(count($myContents->value) > 0) {
foreach ($myContents->value as $imageContent) {
echo "<a href='{$imageContent->webSearchUrl}'><img src='{$imageContent->thumbnailUrl}' width='290' height='200' /></a>";
}
}