X
    Categories: PHP

Bing image search api v5 in php

Bing image search api v5 in php

Bing V2 API will get deprecated from December 9 2016. We need to upgrade our API from v2 to v5. Here we will see what are all the upgrades have done from v2 to v5 and will see how the bing image search
gonna work in v5 using php.

We have Following Kind of Search in Bing API
1. Composite Data Source
2. Image Data Source
3. News Data Source
4. Video Data Source
5. Web Data Source
6. RelatedSearch Data Source
7. SpellingSuggestions Data Source

v5 have changed the query parameters in all the api’s. Here we will see how the Image Data Source API will work.

2. Image Data Source

Endpoints of v2 and v5
The following shows the Version 2 and Version 5 Image endpoints.

Version 2

https://api.datamarket.azure.com/Bing/Search/Image?Query=%27cars%27&$format=json

Version 5

https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=cars
Version 5 also provides the following endpoint which you can use to get images that are trending.
https://api.cognitive.microsoft.com/bing/v5.0/images/trending

Version 5 have newly introduced 2 feature
1. Market codes: allows a country variable to impact search results
https://msdn.microsoft.com/en-us/library/dn783426.aspx


2. Pivot suggestions: Bing can now segment the original search query. For example, if the original query was Microsoft Surface, Bing might segment the query into Microsoft and Surface. We can expose these ‘Segments’ to the user to refine their search
https://msdn.microsoft.com/en-us/library/dn760784.aspx

Here you can find the Query parameter changes.

Example :

<?php
$sURL = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=ms+dhoni&count=5";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sURL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: multipart/form-data',
    'Ocp-Apim-Subscription-Key: xxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$contents = curl_exec($ch);
$myContents = json_decode($contents);
echo '<pre>';
print_r($myContents->value);
?>

Output :

Array
(
    [0] => stdClass Object
        (
            [name] => MS Dhoni | SportsMafia
            [webSearchUrl] => https://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=UaQFAh8MEMVJRrgawiLiVn6kJM-61NRH24KDfwsQUMc&v=1&r=https%3a%2f%2fwww.bing.com%2fimages%2fsearch%3fview%3ddetailv2%26FORM%3dOIIRPO%26q%3dms%2bdhoni%26id%3d9988B7A9456BD76AA2A089B844CCCD19C44E59D1%26simid%3d608052810694918444&p=DevEx,5006.1
            [thumbnailUrl] => https://tse3.mm.bing.net/th?id=OIP.M6f6e09ad30ba68ec4c1ac3ff6b4ef50bH1&pid=Api
            [datePublished] => 2016-10-03T19:54:00
            [contentUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=lrmdM5zq4UiUfxBZAaa_3yvggFOJ4C3Tdagdm_XdeQQ&v=1&r=http%3a%2f%2fsportsmafia.info%2fwp-content%2fuploads%2f2012%2f08%2fMS-Dhoni.jpg&p=DevEx,5008.1
            [hostPageUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=blkOX1en_BN3o0zCngxuEexgW4ERP83kqOe9Ot531hA&v=1&r=http%3a%2f%2fwww.sportsmafia.info%2fcricket%2fmale-cricket-team%2findia%2fms-dhoni%2fms-dhoni%2f&p=DevEx,5007.1
            [contentSize] => 59145 B
            [encodingFormat] => jpeg
            [hostPageDisplayUrl] => www.sportsmafia.info/cricket/male-cricket-team/india/ms-dhoni/ms-dhoni
            [width] => 1203
            [height] => 800
            [thumbnail] => stdClass Object
                (
                    [width] => 300
                    [height] => 199
                )

            [imageInsightsToken] => ccid_b24JrTC6*mid_9988B7A9456BD76AA2A089B844CCCD19C44E59D1*simid_608052810694918444
            [insightsSourcesSummary] => stdClass Object
                (
                    [shoppingSourcesCount] => 0
                    [recipeSourcesCount] => 0
                )

            [imageId] => 9988B7A9456BD76AA2A089B844CCCD19C44E59D1
            [accentColor] => A23729
        )

    [1] => stdClass Object
        (
            [name] => MS Dhoni | SportsMafia
            [webSearchUrl] => https://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=oklOOdJDzCRyQD4EHVgyEM4lE9HkByQ2wqEzDCQh1hk&v=1&r=https%3a%2f%2fwww.bing.com%2fimages%2fsearch%3fview%3ddetailv2%26FORM%3dOIIRPO%26q%3dms%2bdhoni%26id%3d9988B7A9456BD76AA2A0328E3947F3C4003741E5%26simid%3d608046591589484809&p=DevEx,5012.1
            [thumbnailUrl] => https://tse2.mm.bing.net/th?id=OIP.M911ba6e5f3ba7a95045802a6a04022d0H1&pid=Api
            [datePublished] => 2016-10-22T22:38:00
            [contentUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=kwqseOFAgZr2G3ddRDaA_W7davY_efb9GMy50ujAIuE&v=1&r=http%3a%2f%2fsportsmafia.info%2fwp-content%2fuploads%2f2012%2f08%2fMS-Dhoni-Face.jpg&p=DevEx,5014.1
            [hostPageUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=blkOX1en_BN3o0zCngxuEexgW4ERP83kqOe9Ot531hA&v=1&r=http%3a%2f%2fwww.sportsmafia.info%2fcricket%2fmale-cricket-team%2findia%2fms-dhoni%2fms-dhoni%2f&p=DevEx,5013.1
            [contentSize] => 255632 B
            [encodingFormat] => jpeg
            [hostPageDisplayUrl] => www.sportsmafia.info/cricket/male-cricket-team/india/ms-dhoni/ms-dhoni
            [width] => 900
            [height] => 1125
            [thumbnail] => stdClass Object
                (
                    [width] => 240
                    [height] => 300
                )

            [imageInsightsToken] => ccid_kRum5fO6*mid_9988B7A9456BD76AA2A0328E3947F3C4003741E5*simid_608046591589484809
            [insightsSourcesSummary] => stdClass Object
                (
                    [shoppingSourcesCount] => 0
                    [recipeSourcesCount] => 0
                )

            [imageId] => 9988B7A9456BD76AA2A0328E3947F3C4003741E5
            [accentColor] => 0F7BBC
        )

    [2] => stdClass Object
        (
            [name] => ms dhoni 2013 ms dhoni 2013 ms dhoni 2013 ms
            [webSearchUrl] => https://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=S1W-INbQqfkKgWQxDUfwzjnf44V0pvJtEh2i_TJ0H7I&v=1&r=https%3a%2f%2fwww.bing.com%2fimages%2fsearch%3fview%3ddetailv2%26FORM%3dOIIRPO%26q%3dms%2bdhoni%26id%3dCFE6FD9EF52C2780E0641772C8B5EB2B1E6CED6B%26simid%3d608029132554110590&p=DevEx,5018.1
            [thumbnailUrl] => https://tse2.mm.bing.net/th?id=OIP.M4a6c9a6030d795f6ba763bb3e0d1c38dH2&pid=Api
            [datePublished] => 2013-02-22T12:00:00
            [contentUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=huEIUfi2TVeV_wZFeCHh-PcmDN-fvvJPn_PDQ4llAnI&v=1&r=http%3a%2f%2f2.bp.blogspot.com%2f-6soGqm30-qg%2fUSeVMbs0MRI%2fAAAAAAAAD_c%2fWMjsMYuYpiI%2fs1600%2fMs%2bDhoni%2bPhoto%2b2013%2b02.jpg&p=DevEx,5020.1
            [hostPageUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=pHYuCIcvnWpXyWzWddOM7d2tgSZxbCQc0vUIcDeBU6g&v=1&r=http%3a%2f%2fcricketstarswallpapers.blogspot.com%2f2013%2f02%2fms-dhoni-facts-and-fresh-photos-2013.html&p=DevEx,5019.1
            [contentSize] => 28646 B
            [encodingFormat] => jpeg
            [hostPageDisplayUrl] => cricketstarswallpapers.blogspot.com/2013/02/ms-dhoni-facts-and...
            [width] => 559
            [height] => 480
            [thumbnail] => stdClass Object
                (
                    [width] => 300
                    [height] => 257
                )

            [imageInsightsToken] => ccid_SmyaYDDX*mid_CFE6FD9EF52C2780E0641772C8B5EB2B1E6CED6B*simid_608029132554110590
            [insightsSourcesSummary] => stdClass Object
                (
                    [shoppingSourcesCount] => 0
                    [recipeSourcesCount] => 0
                )

            [imageId] => CFE6FD9EF52C2780E0641772C8B5EB2B1E6CED6B
            [accentColor] => B81329
        )

    [3] => stdClass Object
        (
            [name] => Ms Dhoni
            [webSearchUrl] => https://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=TmmYXqomZKlbXGZJdK-hL5MTtAeKtl_eJAaq-lL1-dw&v=1&r=https%3a%2f%2fwww.bing.com%2fimages%2fsearch%3fview%3ddetailv2%26FORM%3dOIIRPO%26q%3dms%2bdhoni%26id%3d71710482377E2F3E80C43D5C95965E854145F9FE%26simid%3d608013202517723769&p=DevEx,5024.1
            [thumbnailUrl] => https://tse1.mm.bing.net/th?id=OIP.M364df44fe09767c494eb9da699d456a0o0&pid=Api
            [datePublished] => 2013-04-18T12:00:00
            [contentUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=9fwqdGytNHu7rlz9bb6JZvPn0ejHY71Crng8Q0Sx56g&v=1&r=http%3a%2f%2fp.imgci.com%2fdb%2fPICTURES%2fCMS%2f153000%2f153010.jpg&p=DevEx,5026.1
            [hostPageUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=FyoCizNPajaDaG1mPUlFESgooZIwSaYq00dHGswvM48&v=1&r=http%3a%2f%2fpakistancricketplayersblogss.blogspot.com%2f2013%2f04%2fms-dhoni.html&p=DevEx,5025.1
            [contentSize] => 220955 B
            [encodingFormat] => jpeg
            [hostPageDisplayUrl] => pakistancricketplayersblogss.blogspot.com/2013/04/ms-dhoni.html
            [width] => 900
            [height] => 1175
            [thumbnail] => stdClass Object
                (
                    [width] => 229
                    [height] => 300
                )

            [imageInsightsToken] => ccid_Nk30T+CX*mid_71710482377E2F3E80C43D5C95965E854145F9FE*simid_608013202517723769
            [insightsSourcesSummary] => stdClass Object
                (
                    [shoppingSourcesCount] => 0
                    [recipeSourcesCount] => 0
                )

            [imageId] => 71710482377E2F3E80C43D5C95965E854145F9FE
            [accentColor] => B1991A
        )

    [4] => stdClass Object
        (
            [name] => ms dhoni one the best style ms dhoni with long
            [webSearchUrl] => https://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=DYccy9IDiShzxnqc5lAKqG0Kq5Z_Dv_WnAhR5waq9nU&v=1&r=https%3a%2f%2fwww.bing.com%2fimages%2fsearch%3fview%3ddetailv2%26FORM%3dOIIRPO%26q%3dms%2bdhoni%26id%3d327DC8B9B33F8344C1BF8F0F2BABBB8DC6DE4FCC%26simid%3d607998191596930441&p=DevEx,5030.1
            [thumbnailUrl] => https://tse2.mm.bing.net/th?id=OIP.M3f3cef0fa030f39906d19630f2648b4bH0&pid=Api
            [datePublished] => 2011-07-26T12:00:00
            [contentUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=0dXbM0WVIOV7uGAzciz6LSz1wgmdSaOuwfXDALoCv5U&v=1&r=http%3a%2f%2f3.bp.blogspot.com%2f-EsOLWyRtZJs%2fTi6xt6lCNbI%2fAAAAAAAAARg%2fDHOchUq7kRE%2fs1600%2fMS-Dhoni-7.jpg&p=DevEx,5032.1
            [hostPageUrl] => http://www.bing.com/cr?IG=B2D622268D384F7AAF8BB73A59FA9F66&CID=2E2CFF7213BA6F4A2FC7F6AA12A86E78&rd=1&h=vQ3_X6pM62qHqvzPAvroeeflwZ6eFfWM2lmG1Z0v31U&v=1&r=http%3a%2f%2fthecricketgames.blogspot.com%2f2011%2f07%2fms-dhoni-free-and-hq-wallpapers.html&p=DevEx,5031.1
            [contentSize] => 62653 B
            [encodingFormat] => jpeg
            [hostPageDisplayUrl] => thecricketgames.blogspot.com/2011/07/ms-dhoni-free-and-hq...
            [width] => 800
            [height] => 600
            [thumbnail] => stdClass Object
                (
                    [width] => 300
                    [height] => 225
                )

            [imageInsightsToken] => ccid_PzzvD6Aw*mid_327DC8B9B33F8344C1BF8F0F2BABBB8DC6DE4FCC*simid_607998191596930441
            [insightsSourcesSummary] => stdClass Object
                (
                    [shoppingSourcesCount] => 0
                    [recipeSourcesCount] => 0
                )

            [imageId] => 327DC8B9B33F8344C1BF8F0F2BABBB8DC6DE4FCC
            [accentColor] => 31313D
        )

)

For more information : https://msdn.microsoft.com/en-US/library/mt707570.aspx

Find a GitHub Repository @https://github.com/marimuthum17/Bing-API-v5

Thanks for reading this article.
If you like this article don’t forget to share and comment.
Thanks!.

Marimuthu: