2017-01-28 3 views
0

Microsoft Azure (인지 서비스)의 새로운 Bing 검색 API에 몇 가지 문제가 있습니다. 여기에 내 코드는, 내가 뭘 하려는지 내가 내 간단한 형태로 만든 결과와 간단한 형태로 API를 호출하는 것입니다,하지만 난 그렇게 몇 가지 문제가 있는데 누군가가 내 코드를보고 어떤 문제가 있는지 볼 수 있습니다 ? 내가 얻는 오류는 $ q 변수를 정의하지 않았지만 코드에서 보게 될 것입니다. 도움을 주셔서 감사합니다, 그것을 감사하십시오!Microsoft인지 서비스 Bing WebSearch API v5.0

PHP :

<?php 
$accountKey = 'account_key'; 

$url = 'https://api.cognitive.microsoft.com/bing/v5.0/search?q='.$q.'&count=10&offset=0&mkt=en-us&safesearch=Moderate'; 

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

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Ocp-Apim-Subscription-Key: $accountKey" 
) 
); 
$context = stream_context_create($opts); 

// Open the file using the HTTP headers set above 
$file = file_get_contents($url, false, $context); 
$jsonobj = json_decode($file); 
echo $file; 
?> 

HTML :

<form method="post" action=""> 
<input name="q" type="text" autocomplete="off" autofocus> 
<input type="submit" name="Search" hidden> 
</form> 
</body> 
</html> 

답변

1

장소 $q = urlencode($_POST['q']); 예를 들어

$accountKey 다음 : 당신은 변수 B라는 한

<?php 
$accountKey = 'account_key'; 
$q = urlencode($_POST['q']); 
$url = 'https://api.cognitive.microsoft.com/bing/v5.0/search?q='.$q.'&count=10&offset=0&mkt=en-us&safesearch=Moderate'; 



$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Ocp-Apim-Subscription-Key: $accountKey" 
) 
); 
$context = stream_context_create($opts); 

// Open the file using the HTTP headers set above 
$file = file_get_contents($url, false, $context); 
$jsonobj = json_decode($file); 
echo $file; 
?> 

선언하다.

+0

결과는 단지 JSON 형식으로되어 있습니다. 결과 스타일을 조금 도와 주실만한 가이드가 있습니까? 또한 페이지를 새로 고치면 검색으로 돌아가서 API를 다시 호출하지 않으므로 어떻게 만들 수 있습니까? – Aceeeeeee

+0

조금만 설명해 주시겠습니까? 검색 결과를 수정해야하는 스타일은 무엇입니까? Muthu17

+0

이후 코드를 작성하십시오. 결과를 스타일 지정하고 Google과 같은보다 일반적인 검색 엔진처럼 만들 수 있습니다. – Aceeeeeee

0

나는 당신의 더 질문을 위해, 당신을위한 코드를 생성 있습니다

내가 페이지를 새로 고칠 때 그렇게는 검색에 날 다시 가져 와서에 호출하지 않습니다 만들 것입니다 방법도

API를 다시 하시겠습니까?

바로 지금 JSON 일 뿐이므로 결과 스타일을 지정하고 Google과 같은보다 일반적인 검색 엔진처럼 만들 수있는 방법이 있습니까?

코드 다음 사항을 고려하십시오 :

<html> 
<body> 
<form method="get" action=""> 
<input name="q" type="text" autocomplete="off" value="<?=$_GET['q']?>" autofocus> 
<input type="submit" hidden> 
</form> 
</body> 
</html> 

<?php 
$accountKey = '<accountKey>'; 
$q = @urlencode($_GET['q']); 
if($q){ 
    $url = 'https://api.cognitive.microsoft.com/bing/v5.0/search?q='.$q.'&count=10&offset=0&mkt=en-us&safesearch=Moderate'; 
    $opts = array(
     'http'=>array(
     'method'=>"GET", 
     'header'=>"Ocp-Apim-Subscription-Key: $accountKey" 
    ) 
    ); 
    $context = stream_context_create($opts); 

    // Open the file using the HTTP headers set above 
    $file = file_get_contents($url, false, $context); 
    $jsonobj = json_decode($file); 
    echo ('<ul ID="resultList">'); 
    foreach ($jsonobj->webPages->value as $value) { 
     echo ('<li class="resultlistitem"><a href="' . $value->url . '">'.$value->name.'</a>'); 
     if(property_exists($value,'image')){ 
      echo ('<img src="' . $value->image->contentUrl . '"></li>'); 
     } 
    } 
    echo ("</ul>"); 
    } 

?> 
+0

답변을 주셔서 감사합니다. :) 그러나 URL은 빙하고 있습니다. 링크를 실제 URL로 가져 오려면 어떻게해야합니까? 예를 들어 "google"을 검색하면 google.com이 아니며 google.com으로 리디렉션되는 빙 링크입니다. 내가 어떻게 바꿀 수 있니? – Aceeeeeee

+0

Bing 검색 API를 사용할 때 결과는 리디렉션 URL이어야합니다. 의도적으로 설계된 것입니다. –

0

새로운 인식 API는 계정 키와 등록 키가 필요합니다. 두 가지가 모두 포함될 때까지 계속 오류가 발생합니다.

관련 문제