البدء السريع: قم بإجراء بحث عن الأخبار باستخدام PHP وواجهة برمجة تطبيقات REST للبحث في أخبار Bing

تحذير

في 30 أكتوبر 2020، انتقلت واجهات برمجة التطبيقات بحث Bing من خدمات Azure الذكاء الاصطناعي إلى خدمات بحث Bing. تتوفر هذه الوثائق للرجوع إليها فحسب. للحصول على وثائق محدثة، راجع الوثائق الخاصة ببحث Bing API . بالنسبة للإرشادات حول إنشاء موارد Azure جديدة لبحث Bing يرجى مراجعة إنشاء مورد Bing Search من خلال Azure Marketplace.

قم باستخدام هذا البدء السريع لإجراء مكالمتك الأولى لواجهة برمجة تطبيقات بحث أخبار Bing. يرسل تطبيق PHP البسيط هذا استعلام بحث إلى API ويعرض استجابة JSON.

على الرغم من أن هذا التطبيق مكتوب بلغة PHP، فإن API هي خدمة ويب RESTful متوافقة مع معظم لغات البرمجة.

المتطلبات الأساسية

  • نظام PHP 5.6 أو أحدث

أنشئ مورد Azure

ابدأ باستخدام Bing News Search API من خلال إنشاء أحد الموارد التالية لـ Azure:

مورد Bing Search v7

  • متوفر من خلال بوابة Azure لطالما لم تحذف المورد.
  • استخدم مستوى التسعير المجاني لتجربة الخدمة وترقيتها لاحقاً إلي المستوى المدفوع الخاص بعملية الإنتاج.

موارد متعددة الخدمات

  • متوفر من خلال مدخل Azure إلى أن تقوم بحذف المورد.
  • استخدم نفس المفتاح ونقطة النهاية لتطبيقاتك، عبر خدمات Azure الذكاء الاصطناعي المتعددة.

لمزيد من المعلومات، راجع تسعير خدمات Azure الذكاء الاصطناعي - بحث Bing API.

شغّل التطبيق

لتشغيل هذا التطبيق، قم باتباع الخطوات التالية:

  1. قم بتمكين دعم HTTP الآمن في ملف php.iniالخاص بك عن طريق إلغاء التعليق على سطر;extension=php_openssl.dll، كما هو موضح في تعليق التعليمة البرمجية.
  2. قم بإنشاء مشروع PHP جديد في IDE أو المحرر المفضل لديك.
  3. قم بإضافة الرمز الوارد أدناه.
  4. قم باستبدال قيمة accessKey بمفتاح وصول صالح لاشتراكك.
  5. يمكنك استخدام نقطة النهاية العامة في التعليمات البرمجية التالية، أو استخدام نقطة نهاية المجال الفرعي المخصص المعروضة في مدخل Microsoft Azure لموردك.
  6. قم بتشغيل البرنامج.
<?php

// NOTE: Be sure to uncomment the following line in your php.ini file.
// ;extension=php_openssl.dll

// **********************************************
// *** Update or verify the following values. ***
// **********************************************

// Replace the accessKey string value with your valid access key.
$accessKey = 'enter key here';

// Verify the endpoint URI.  At this writing, only one endpoint is used for Bing
// search APIs.  In the future, regional endpoints may be available.  If you
// encounter unexpected authorization errors, double-check this value against
// the endpoint for your Bing Search instance in your Azure dashboard.
$endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/news/search';

$term = 'Microsoft';

function BingNewsSearch ($url, $key, $query) {
    // Prepare HTTP request
    // NOTE: Use the key 'http' even if you are making an HTTPS request. See:
    // https://php.net/manual/en/function.stream-context-create.php
    $headers = "Ocp-Apim-Subscription-Key: $key\r\n";
    $options = array ('http' => array (
                          'header' => $headers,
                          'method' => 'GET' ));

    // Perform the Web request and get the JSON response
    $context = stream_context_create($options);
    $result = file_get_contents($url . "?q=" . urlencode($query), false, $context);

    // Extract Bing HTTP headers
    $headers = array();
    foreach ($http_response_header as $k => $v) {
        $h = explode(":", $v, 2);
        if (isset($h[1]))
            if (preg_match("/^BingAPIs-/", $h[0]) || preg_match("/^X-MSEdge-/", $h[0]))
                $headers[trim($h[0])] = trim($h[1]);
    }

    return array($headers, $result);
}

print "Searching news for: " . $term . "\n";

list($headers, $json) = BingNewsSearch($endpoint, $accessKey, $term);

print "\nRelevant Headers:\n\n";
foreach ($headers as $k => $v) {
    print $k . ": " . $v . "\n";
}

print "\nJSON Response:\n\n";
echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
?>

مثال على استجابة JSON

يتم إرجاع استجابة ناجحة في JSON كما هو موضح في المثال التالي:

{
   "_type": "News",
   "readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/news\/search?q=Microsoft",
   "totalEstimatedMatches": 36,
   "sort": [
      {
         "name": "Best match",
         "id": "relevance",
         "isSelected": true,
         "url": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/news\/search?q=Microsoft"
      },
      {
         "name": "Most recent",
         "id": "date",
         "isSelected": false,
         "url": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/news\/search?q=Microsoft&sortby=date"
      }
   ],
   "value": [
      {
         "name": "Microsoft to open flagship London brick-and-mortar retail store",
         "url": "http:\/\/www.contoso.com\/article\/microsoft-to-open-flagshi...",
         "image": {
            "thumbnail": {
               "contentUrl": "https:\/\/www.bing.com\/th?id=ON.F9E4A49EC010417...",
               "width": 220,
               "height": 146
            }
         },
         "description": "After years of rumors about Microsoft opening a brick-and-mortar...", 
         "about": [
           {
             "readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entiti...", 
             "name": "Microsoft"
           }, 
           {
             "readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entit...", 
             "name": "London"
           }
         ], 
         "provider": [
           {
             "_type": "Organization", 
             "name": "Contoso"
           }
         ], 
          "datePublished": "2017-09-21T21:16:00.0000000Z", 
          "category": "ScienceAndTechnology"
      }, 

      . . .
      
      {
         "name": "Microsoft adds Availability Zones to its Azure cloud platform",
         "url": "https:\/\/contoso.com\/2017\/09\/21\/microsoft-adds-availability...",
         "image": {
            "thumbnail": {
               "contentUrl": "https:\/\/www.bing.com\/th?id=ON.0AE7595B9720...",
               "width": 700,
               "height": 466
            }
         },
         "description": "Microsoft has begun adding Availability Zones to its...",
         "about": [
            {
               "readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entities\/a093e9b...",
               "name": "Microsoft"
            },
            {
               "readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entities\/cf3abf7d-e379-...",
               "name": "Windows Azure"
            },
            {
               "readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entities\/9cdd061c-1fae-d0...",
               "name": "Cloud"
            }
         ],
         "provider": [
            {
               "_type": "Organization",
               "name": "Contoso"
            }
         ],
         "datePublished": "2017-09-21T09:01:00.0000000Z",
         "category": "ScienceAndTechnology"
      }
   ]
}

الخطوات التالية