【问题标题】:Azure won't cURL a google site search xmlAzure 不会卷曲谷歌网站搜索 xml
【发布时间】:2014-08-29 15:04:44
【问题描述】:

我正在运行付费 Google 站点搜索的 XML 到 PHP 安装。

https://code.google.com/p/google-csbe-example/downloads/detail?name=gss.php&can=2&q=

我的初始实现在 LAMP 服务器上完美运行,但是我现在必须在 Windows Azure 平台上运行 PHP 环境。

$url 变量似乎没有通过 cURL 传递,因为 $result 变量返回为 NULL

$url = 'https://www.google.com/cse?cx=' . $your_cx_number . '&client=google-csbe&output=xml_no_dtd&q=' . $q;
if(isset($start)){
    $url .= '&start=' . $start;
}

如果我将 $url 的值修改为不同的远程 xml 文件,并稍微调整输出结构,我会得到预期的结果。

我尝试了几个不同的故障排除步骤,包括:

  • cURL:备用 xml 提要呈现
  • simplexml:备用 RSS 提要呈现
  • 权限:不需要网站权限 google cse 仪表板
  • 备用天蓝色站点:测试失败
  • 备用 LAMP 托管网站:测试成功
  • 备用搜索设置:这无效
  • 是被谷歌屏蔽的域:不这么认为
  • url 查询被阻止:不确定这是否会导致任何问题

我被难住了。

任何帮助将不胜感激。 谢谢!

这是完整的代码(减去 cx 编号):

<?php 
//ini_set('display_startup_errors',1);
//ini_set('display_errors',1);
//error_reporting(-1);

$q = $_GET['q'];
$q = urlencode($q);

//WRAPPED IN A IF STATEMENT SO PROMOTED RESULTS SHOW UP ON THE FIRST PAGE
if(isset($_GET['start'])){
   $start = $_GET['start'];
}

$your_cx_number = 'enter_your_paid_google_site_search_cx_number';

$url = 'https://www.google.com/cse?cx=' . $your_cx_number . '&client=google-csbe&output=xml_no_dtd&q=' . $q;
if(isset($start)){
    $url .= '&start=' . $start;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 30s
curl_setopt($ch, CURLOPT_HTTPGET, true); // set POST method
//curl_setopt($ch, CURLOPT_POSTFIELDS, "postparam1=postvalue"); // add POST fields

//submit the xml request and get the response
$result = curl_exec($ch);
curl_close($ch);

//now parse the xml with 
$xml = simplexml_load_string($result);
$START = $xml->RES['SN'];
$END = $xml->RES['EN'];
$RESULTS = $xml->RES->M;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search results</title>
</head>
<body>
<form action="search-test.php" id="searchform" >
    <input type="text" name="q" placeholder="Search..." <?php if(isset($_GET['q'])) { echo 'value="' . $_GET['q']  . '"' ; }?> id="search-text" size="25" autocomplete="off" />
    <input type="submit"  id="search-button" title="Search" value="" />
</form>
<p>The url of the <a href="<?php echo $url ?>">XML output</a></p>
    <?php
//extract the title, link and snippet for each result
if ($xml->RES->R) {
    foreach ($xml->RES->R as $item) {
        $title = $item->T;
        $link = $item->U;
        $snippet = $item->S;
        echo    '<h3><a href="' . $link . '">' . $title . '</a></h3>
                 <p><a href="' . $link . '">' . $title . '</a></p>
                 <p>' . $snippet . '</p>
                 <hr />';
    }
}
?>
</body>
</html>

【问题讨论】:

    标签: php xml azure curl google-custom-search


    【解决方案1】:

    cURL 报错返回错误码:60

    Curl 错误:60 - SSL 证书问题:无法获取本地颁发者证书

    搜索类似的错误提供了解决方案 HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

    在行中添加:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    

    完整的 cURL 函数现在是:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 30s
    curl_setopt($ch, CURLOPT_HTTPGET, true); // set POST method
    
    $result = curl_exec($ch);
    if(curl_errno($ch)){ 
            echo 'Curl error: ' . curl_errno($ch) . ' - ' .curl_error($ch); 
            $info = curl_getinfo($ch); 
            if (empty($info['http_code'])) { 
                die("No HTTP code was returned"); 
            } else { 
                    echo $info['http_code']; 
            } 
    } 
    curl_close($ch);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多