0

作業中のアプリケーションを古い Bing API から新しい API に移植しています。新しいものが PHP でどのように機能するかについてのいくつかの投稿を見てきましたが、認証の問題が発生しています。

これは、URL によって返されるエラーです。

Warning: file_get_contents(https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27%27) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 The authorization type you provided is not supported. Only Basic and OAuth are supported in /home/krem81/public_html/classes/class.BingSearch.php on line 178

次に、これは API とやり取りするために使用している関数です。

$accountKey = $this->Appid;

$ServiceRootURL =  "https://api.datamarket.azure.com/Bing/Search/";

$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';

$context = stream_context_create(array(
    'http' => array(
        'request_fulluri' => true,
        'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    )
));

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

echo($request);

$this->response = file_get_contents($request, 0, $context);

$this->results = json_decode($this->response);

これを試してから、私も試しました

    $accountKey = $this->Appid;

    $ServiceRootURL =  "https://api.datamarket.azure.com/Bing/Search/";

    $WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';

    $request = $WebSearchURL . urlencode( '\'' . $this->keyword . '\'');

    echo($request);

    $process = curl_init($request);
    curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($process, CURLOPT_USERPWD,  $accountKey . ":" . $accountKey);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);

    $this->response = curl_exec($process);

    var_dump($this->response);

    $this->results = json_decode($this->response);

    var_dump($this->results);

認証が失敗する原因となる可能性があるものについて、誰か考えがありますか?

4

3 に答える 3

1

アクセスキーを「XXXX」に置き換えるだけの検索APIの動作例を次に示します。cURLを使用して動作させるのにかなりの時間を無駄にしましたが、ローカルで「CURLOPT_SSL_VERIFYPEER」の原因で失敗していました:(

$url = 'https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, "username:XXXX");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($process);

# Deliver
return $response;

# Have a great day!
curl_close($process);
于 2013-04-17T13:03:00.437 に答える
0

別のチーム メンバーが変数の名前を変更したので、すぐに確認する必要があったと思います。$this->Appidだったはず$this->APPIDです。

コメントで ManseUK に言ったように、Microsoft は欠落しているアプリ ID に対してより良いエラー メッセージを提供できますが、問題を絞り込むのに役立つ「無効な appid」の行に沿った何か

于 2012-08-07T10:41:12.770 に答える
0

トラフィックを減らして速度を上げるための提案: auth で名前を
追加および削除します。は、あなたが必要とすることすべてです。Accept-Encoding: gzipbase64_encode(':' . $this->APPID)

于 2015-02-24T09:21:09.020 に答える