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は検索に必要な文字列です。
ありがとうございました!
が更新答え、助けの希望を参照してください@Paul。 – Vincent
ありがとうございました!私は使用していたはずです。 +の代わりに... PHPに精通していない。 – Paul
は私たちの最高の状態になります – Vincent