2016-04-03 12 views
1

ここで質問があります。Bing Search APIからデータを取得するためにPHPスクリプトに変数を転送しようとしています。Bing Searchを使用するとURLエンコーディングが失敗する

私は、次のAJAXコードを使用:

var bingquery = 'bingquery=' + $('#query').val(); 
    console.log(bingquery); 

    $.ajax({ 
     method: "POST", 
     url: "hw8.php", 
     dataType: "json", 
     data: bingquery, 
     success: function(jsondata){ 
      console.log('***Test for News Feeds***'); 
      console.log(jsondata); 
     } 
     }); 

をそして、私のPHPは次のとおりです。

if (isset($_POST["bingquery"])){ 
    // Replace this value with your account key 
    $accountKey = '***myaccountkey***'; 

    $WebSearchURL = 'https://api.datamarket.azure.com/Bing/Search/v1/' + 'News?$format=json&Query='; 

    $cred = sprintf('Authorization: Basic %s', base64_encode($accountKey . ":" . $accountKey)); 

    $context = stream_context_create(array(
     'http' => array(
      'header' => $cred 
     ) 
    )); 

    $request = $WebSearchURL . urlencode('\'' . $_POST["bingquery"] . '\''); 

//if I hard code the request URL here, it does work. 

     $response = file_get_contents($request, 0, $context); 

     echo $response; 

    } 

私のURLエンコードに問題がある場合、私は疑問に思いますか?コンソールはfile_get_contents(0%27MYSYMBOL%27)が失敗すると言うので、MYSYMBOLは検索に必要な文字列です。

ありがとうございました!

答えて

1

エンコーディングに関して何も問題はありません。urlencodeは、入力文字列urlを安全にすることになっており、それはまさにそれが行っていることです。\はURLで特別な意味を持ち、 。あなたがPHP .で、二つの文字列を加算している

UPDATE

は、以下の変更を行い、2つの文字列を連結するために使用され、

$WebSearchURL = 'https://api.datamarket.azure.com/Bing/Search/v1/News'; 

$request = $WebSearchURL .'?Query='.urlencode($_POST["bingquery"]).'&$format=json; 
+0

が更新答え、助けの希望を参照してください@Paul。 – Vincent

+0

ありがとうございました!私は使用していたはずです。 +の代わりに... PHPに精通していない。 – Paul

+0

は私たちの最高の状態になります – Vincent

関連する問題