2014-08-29 65 views
1

我正在運行XML到PHP安裝的付費谷歌網站搜索。Azure不會捲曲一個谷歌網站搜索xml

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文件,用少許調整產出結構,我得到預期的結果。

我曾嘗試幾種不同的故障處理步驟,包括:

  • 捲曲:備用XML飼料呈現
  • simplexml的:交替RSS頻道呈現
  • 權限:網站權限不需要谷歌CSE儀表板
  • 替代天青網站:測試和失敗
  • 備用LAMP託管網站:測試成功
  • 備用搜索設置:這是沒有效果的
  • 是阻止谷歌域名:不這麼認爲
  • 網址查詢阻止:這是否是引起任何問題

我難倒不知道。

任何幫助將不勝感激。 謝謝!

下面是完整的代碼(減去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> 

回答

1

捲曲錯誤報告返回錯誤代碼:60

Curl error: 60 - SSL certificate problem: unable to get local issuer certificate

一種類似的錯誤搜索所提供的解決方案 HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

加入該行:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

全捲曲功能現在是:

$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);