Avatar of Pedro Chagas
Pedro Chagas
Flag for Portugal asked on

Organize data from Google news API using php

Hi E's, I need to organize some data from a query of google news API, and I want to list or organize and put some of that received information into a php array. In this case I want put the publishers of the news into array.
I try this code:
<?
$jsonString = file_get_contents("https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=algarve%20turismo&hl=pt&ned=pt-PT_pt&callback=foo");
$json = json_decode($jsonString);
$links = array();
foreach($json->foo as $item) {
  $links[] = $item->publisher;
}
?>
<pre>
<?
print_r($links);
?>

Open in new window

But the result is:
Warning: Invalid argument supplied for foreach() in /home/pedro/public_html/biblioteca/tutoriais/1140/exemplo.php on line 5
Array
(
)

Open in new window


What I do wrong?

The best regards, JC
PHPJavaScript

Avatar of undefined
Last Comment
John Kawakami

8/22/2022 - Mon
SOLUTION
John Kawakami

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Pedro Chagas

ASKER
Hi @jk2001, I remove the callback, and the result is exactelly the same.
The line has been change to:
$jsonString = file_get_contents("https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=algarve%20turismo&hl=pt&ned=pt-PT_pt");

Open in new window


The output in the browser is:
{"responseData": {"results":[{"GsearchResultClass":"GnewsSearch","clusterUrl":"","content":"\u003cb\u003e...\u003c/b\u003e disse o «patrão» da GIATA Decorreu, no passado dia 11 de maio, num hotel da Praia da Rocha, um congresso organizado pela VIR, especialista em viagens online na Alemanha, em conjunto com o \u003cb\u003eTurismo\u003c/b\u003e do \u003cb\u003eAlgarve\u003c/b\u003e ea Associação de \u003cb\u003eTurismo\u003c/b\u003e de Portimão.","unescapedUrl":"http://www.barlavento.pt/index.php/noticia?id\u003d53035","url":"http%3A%2F%2Fwww.barlavento.pt%2Findex.php%2Fnoticia%3Fid%3D53035","title":"O \u003cb\u003eAlgarve\u003c/b\u003e visto pelos \u003cb\u003eturistas\u003c/b\u003e alemães","titleNoFormatting":"O Algarve visto pelos turistas alemães","location":"","publisher":"Barlavento Online","publishedDate":"Mon, 21 May 2012 15:19:41 -0700","signedRedirectUrl":"http://news.google.com/news/url?sa\u003dT\u0026ct\u003dpt/0-0-0\u0026fd\u003dS\u0026url\u003dhttp://www.barlavento.pt/index.php/noticia%3Fid%3D53035\u0026cid\u003d0\u0026ei\u003dFNO7T6irCdmu-Abs8AE\u0026usg\u003dAFQjCNGnD55PMcrhnaOYfFboqRWEcBEWAQ","language":"pt","image":{"url":"http://www.barlavento.pt/upload/multimedia/TN1274367244.jpg","tbUrl":"http://nt3.ggpht.com/news/tbn/Q8YJjOHsNTYJ","originalContextUrl":"http://www.barlavento.pt/index.php/noticia?id\u003d53035","publisher":"Barlavento Online","tbWidth":80,"tbHeight":45}},{"GsearchResultClass":"GnewsSearch","clusterUrl":"","content":"O professor da Escola de Hotelaria e \u003cb\u003eTurismo\u003c/b\u003e da Universidade do \u003cb\u003eAlgarve\u003c/b\u003e, que está a organizar um congresso sobre o tema já agendado para Novembro, garante que se trata de um mercado em crescimento por toda a Europa, em parte devido ao envelhecimento da \u003cb\u003e...\u003c/b\u003e","unescapedUrl":"http://www.rcmpharma.com/actualidade/saude/21-05-12/turismo-de-saude-algarve-esta-perder-batalha-do-turista-que-quer-tratamen","url":"http%3A%2F%2Fwww.rcmpharma.com%2Factualidade%2Fsaude%2F21-05-12%2Fturismo-de-saude-algarve-esta-perder-batalha-do-turista-que-quer-tratamen","title":"\u003cb\u003eTurismo\u003c/b\u003e de saúde: \u003cb\u003eAlgarve\u003c/b\u003e está a perder batalha do turista que \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Turismo de saúde: Algarve está a perder batalha do turista que ...","location":"","publisher":"RCM Pharma","publishedDate":"Mon, 21 May 2012 00:15:18 -0700","signedRedirectUrl":"http://news.google.com/news/url?sa\u003dT\u0026ct\u003dpt/0-1-0\u0026fd\u003dS\u0026url\u003dhttp://www.rcmpharma.com/actualidade/saude/21-05-12/turismo-de-saude-algarve-esta-perder-batalha-do-turista-que-quer-tratamen\u0026cid\u003d0\u0026ei\u003dFNO7T6irCdmu-Abs8AE\u0026usg\u003dAFQjCNF8GzDulapi8ZYH6zEVY0bkdwoiDw","language":"pt"},{"GsearchResultClass":"GnewsSearch","clusterUrl":"http://news.google.com/news/story?ncl\u003dd_RNVOrW0yaDUPM41guC1et9dZvKM\u0026hl\u003dpt-PT\u0026ned\u003dpt-PT_pt","content":"Centenas de estrangeiros estão a escolher os hospitais privados do \u003cb\u003eAlgarve\u003c/b\u003e para fazer intervenções cirúrgicas, mas o \u003cb\u003eturismo\u003c/b\u003e médico ainda não \u0026quot;disparou\u0026quot;, porque o Governo não o tem como prioritário, disse à Lusa fonte do setor. \u0026quot;As clínicas e hospitais \u003cb\u003e...\u003c/b\u003e","unescapedUrl":"http://www.dn.pt/inicio/portugal/interior.aspx?content_id\u003d2532205","url":"http%3A%2F%2Fwww.dn.pt%2Finicio%2Fportugal%2Finterior.aspx%3Fcontent_id%3D2532205","title":"\u003cb\u003eAlgarve\u003c/b\u003e: sol, praia e... cirurgias","titleNoFormatting":"Algarve: sol, praia e... cirurgias","location":"","publisher":"Diário de Notícias - Lisboa","publishedDate":"Sat, 19 May 2012 03:44:43 -0700","signedRedirectUrl":"http://news.google.com/news/url?sa\u003dT\u0026ct\u003dpt/0-2-1\u0026fd\u003dS\u0026url\u003dhttp://www.dn.pt/inicio/portugal/interior.aspx%3Fcontent_id%3D2532205\u0026cid\u003d8797496449174\u0026ei\u003dFNO7T6irCdmu-Abs8AE\u0026usg\u003dAFQjCNEm9HWEM902Oe4vX-O5KNBu61kHdw","language":"pt","image":{"url":"http://www.dn.pt/storage/DN/2012/big/ng1945358.jpg?type\u003dbig\u0026pos\u003d0","tbUrl":"http://nt1.ggpht.com/news/tbn/2ZXRkh8JL38J","originalContextUrl":"http://www.dn.pt/inicio/portugal/interior.aspx?content_id\u003d2532205","publisher":"Diário de Notícias - Lisboa","tbWidth":80,"tbHeight":53},"relatedStories":[{"unescapedUrl":"http://www.regiao-sul.pt/noticia.php?refnoticia\u003d127586","url":"http%3A%2F%2Fwww.regiao-sul.pt%2Fnoticia.php%3Frefnoticia%3D127586","title":"\u003cb\u003eTurismo\u003c/b\u003e de Saúde: \u003cb\u003eAlgarve\u003c/b\u003e está a perder batalha do turista que \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Turismo de Saúde: Algarve está a perder batalha do turista que ...","location":"","publisher":"Região Sul","publishedDate":"Sat, 19 May 2012 03:42:13 -0700","signedRedirectUrl":"http://news.google.com/news/url?sa\u003dT\u0026ct\u003dpt/0-2-0\u0026fd\u003dS\u0026url\u003dhttp://www.regiao-sul.pt/noticia.php%3Frefnoticia%3D127586\u0026cid\u003d8797496449174\u0026ei\u003dFNO7T6irCdmu-Abs8AE\u0026usg\u003dAFQjCNHaDx0BVGRh3FGgj_2P_38kktoqCA","language":"pt"}]},{"GsearchResultClass":"GnewsSearch","clusterUrl":"","content":"Centenas de estrangeiros estão a escolher os hospitais privados do \u003cb\u003eAlgarve\u003c/b\u003e para fazer intervenções cirúrgicas, mas o \u003cb\u003eturismo\u003c/b\u003e médico ainda não \u0026quot;disparou\u0026quot;, porque o Governo não o tem como prioritário, disse à Lusa fonte do sector.","unescapedUrl":"http://www.dnoticias.pt/actualidade/economia/325263-turistas-escolhem-algarve-para-cirurgias-mas-promocao-podia-fazer-dispar","url":"http%3A%2F%2Fwww.dnoticias.pt%2Factualidade%2Feconomia%2F325263-turistas-escolhem-algarve-para-cirurgias-mas-promocao-podia-fazer-dispar","title":"\u003cb\u003eTuristas\u003c/b\u003e escolhem \u003cb\u003eAlgarve\u003c/b\u003e para cirurgias mas promoção podia fazer \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Turistas escolhem Algarve para cirurgias mas promoção podia fazer ...","location":"","publisher":"Diário de Notícias - Funchal","publishedDate":"Sat, 19 May 2012 03:41:10 -0700","signedRedirectUrl":"http://news.google.com/news/url?sa\u003dT\u0026ct\u003dpt/0-3-0\u0026fd\u003dS\u0026url\u003dhttp://www.dnoticias.pt/actualidade/economia/325263-turistas-escolhem-algarve-para-cirurgias-mas-promocao-podia-fazer-dispar\u0026cid\u003d8797496293053\u0026ei\u003dFNO7T6irCdmu-Abs8AE\u0026usg\u003dAFQjCNG93r7_yf76__sS-NSPaVvX6UVatA","language":"pt","image":{"url":"http://www.dnoticias.pt/sites/default/files/imagecache/body_300/GI06486_Small_0.JPG","tbUrl":"http://nt1.ggpht.com/news/tbn/SfRCXV4cRAEJ","originalContextUrl":"http://www.dnoticias.pt/actualidade/economia/325263-turistas-escolhem-algarve-para-cirurgias-mas-promocao-podia-fazer-dispar","publisher":"Diário de Notícias - Funchal","tbWidth":80,"tbHeight":53}}],"cursor":{"pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"289","currentPageIndex":0,"moreResultsUrl":"http://news.google.com/nwshp?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026q\u003dalgarve+turismo\u0026ned\u003dpt-PT_pt\u0026start\u003d0"}}, "responseDetails": null, "responseStatus": 200}

Open in new window

The goal is put in php array the "publisher": data/information, but I still do something wrong!

Can you see what is wrong in my code?

Regards, JC
ASKER CERTIFIED SOLUTION
John Kawakami

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck