Php YQL:获取不支持的http协议错误
当我试图通过cURL调用YQL时,我得到了以下错误 不支持HTTP版本 描述:web服务器“engine1.yql.vip.bf1.yahoo.com”使用的HTTP协议版本不受支持 下面是使用的代码Php YQL:获取不支持的http协议错误,php,http,curl,yql,http-protocols,Php,Http,Curl,Yql,Http Protocols,当我试图通过cURL调用YQL时,我得到了以下错误 不支持HTTP版本 描述:web服务器“engine1.yql.vip.bf1.yahoo.com”使用的HTTP协议版本不受支持 下面是使用的代码 // URL $URL = "https://query.yahooapis.com/v1/public/yql?q=select * from html where url=\"http://www.infibeam.com/Books/search?q=978817991755
// URL
$URL = "https://query.yahooapis.com/v1/public/yql?q=select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[@class='infiPrice amount price']/text()\"&format=json";
// set url
curl_setopt($ch, CURLOPT_URL, $URL);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
echo $output;
?>
从thr浏览器调用相同的URL可以正常工作
*从html何处开始
url=”http://www.infibeam.com/Books/search?q=9788179917558"及
xpath=“//span[@class='infiPrice amount price']/text()”&format=json
有人能告诉我代码中有什么错误吗?问题可能是因为您提供给cURL的url无效。您需要准备/编码查询字符串的各个值,以便在url中使用 您可以使用以下方法完成此操作:
在这种情况下,我只对
q
的值进行了编码,因为格式不包含url中不能使用的字符,但通常情况下,您会对任何不知道或不控制的值进行编码。好的,我明白了。。问题在于https。使用以下代码段进行调试
if (false === ($data = curl_exec($ch))) {
die("Eek! Curl error! " . curl_error($ch));
}
下面添加了默认接受SSL证书的代码
$options = array(CURLOPT_URL => $URL,
CURLOPT_HEADER => "Content-Type:text/xml",
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => TRUE
);
完整的代码在这里
<?php
// create curl resource
$ch = curl_init();
// URL
$q = urlencode("select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[@class='infiPrice amount price']/text()\"");
$URL = "https://query.yahooapis.com/v1/public/yql?q={$q}&format=json";
echo "URL is ".$URL;
$ch = curl_init();
//Define curl options in an array
$options = array(CURLOPT_URL => $URL,
CURLOPT_HEADER => "Content-Type:text/xml",
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => TRUE
);
//Set options against curl object
curl_setopt_array($ch, $options);
//Assign execution of curl object to a variable
$data = curl_exec($ch);
echo($data);
//Pass results to the SimpleXMLElement function
//$xml = new SimpleXMLElement($data);
echo($data);
if (false === ($data = curl_exec($ch))) {
die("Eek! Curl error! " . curl_error($ch));
}
if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
die("Oh dear, no 200 OK?!");
}
//Close curl object
curl_close($ch);
0,
CURLOPT_RETURNTRANSFER=>TRUE
);
//针对卷曲对象设置选项
curl_setopt_数组($ch$options);
//将curl对象的执行分配给变量
$data=curl\u exec($ch);
回波(数据);
//将结果传递给SimpleXMLElement函数
//$xml=新的simplexmlement($data);
回波(数据);
if(false==($data=curl\u exec($ch))){
die(“Eek!Curl error!”.Curl_error($ch));
}
if(200!==(int)curl\u getinfo($ch,CURLINFO\u HTTP\u代码)){
死(“哦,天哪,没有200行吗?!”);
}
//闭合卷曲对象
卷曲关闭($ch);
?>我仍然看不到任何响应:(@Anil如果您使用http
而不是https
是否有效,并且错误消息是否相同?@Anil如果您确实需要,可以为https设置/取消设置一些附加选项:
<?php
// create curl resource
$ch = curl_init();
// URL
$q = urlencode("select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[@class='infiPrice amount price']/text()\"");
$URL = "https://query.yahooapis.com/v1/public/yql?q={$q}&format=json";
echo "URL is ".$URL;
$ch = curl_init();
//Define curl options in an array
$options = array(CURLOPT_URL => $URL,
CURLOPT_HEADER => "Content-Type:text/xml",
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => TRUE
);
//Set options against curl object
curl_setopt_array($ch, $options);
//Assign execution of curl object to a variable
$data = curl_exec($ch);
echo($data);
//Pass results to the SimpleXMLElement function
//$xml = new SimpleXMLElement($data);
echo($data);
if (false === ($data = curl_exec($ch))) {
die("Eek! Curl error! " . curl_error($ch));
}
if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
die("Oh dear, no 200 OK?!");
}
//Close curl object
curl_close($ch);