CSE Google Custom Page api show 50 results PHP? - php

I'm trying to get the first 50 results of the Google CSE API with the following PHP code.
The problem is that it combines the two pages sow the results are getting messed up like the first position is the one of the first page and the second position is the second of the second page and so on. Can someone tell me what I am doing wrong here?
What I actually want to do is get the first 50 results in a array, but the code below gives me mixed results.
$apiKey = "theapikey";
$query = "news";
for ($i = 1; $i <= 5; $i++) {
$ch = curl_init();
$request = "https://www.googleapis.com/customsearch/v1?q=" . urlencode( "$query" ) . "&cx=013594553343653397533:q-qkkaltmay" ."&key=" . $apiKey . "&start=" . $i;
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
$output = json_decode($output);
foreach( $output->items as $result ) {
$url = $result->link;
${"items" . $i}[] = $url;
}
}
echo json_encode($items1);

It looks like you are adding each set of 10 results into a separate array, so $items1 has the first 10 results, $items2 has the next 10, etc. If you want all 50 results in a single array, there is no need to use an index in the array's name.
Also, the "start" parameter is the number of the result you want, not the number of the result set - so you want the first query to start at 1, the second to start at 11, the third at 21, etc.
You may also want to check that there is something in the result before adding it to your array.
I might do something more like so:
$apiKey = "theapikey";
$query = "news";
$items = array();
for ($i = 1; $i <= 5; $i++) {
$ch = curl_init();
$request = "https://www.googleapis.com/customsearch/v1?" .
"q=" . urlencode( "$query" ) .
"&cx=013594553343653397533:q-qkkaltmay" .
"&key=" . $apiKey .
"&start=" . ( ($i - 1)*10 + 1 );
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
$output = json_decode($output);
foreach( $output->items as $result ) {
if ($url = $result->link && trim($url)) $items[] = $url;
}
}
echo json_encode($items);
Finally, a couple caveats:
There is an existing question about whether this JSON api is deprecated and possibly going away.
Each query for the next 10 results counts against your quota. If you are worried about running out of queries each month, or if you are paying for quota increases, you may want to consider only retrieving what you need.

Related

How to get all comments from a Youtube video

Firstly, I looked all of other title. All of them out of date. I mean, they are use old api.
I write a code to list all comments with their nextPageToken
<?php
$url = "SE0wDh_pILk"; // Youtube video ID
$ytkey = "IzaSyCaRXmJ9XDC4XucAZCzXx7hisCtYEH0mNs"; //"IzaSyBuu-rnbmPAj1DjR6WmyxGmpmQKz8aTXbw" Your api key
$nextPage = ""; // Next Page Token for get comments of next Page.
//$i =0; // DO NOT CHANGE
for ($i = 0; $i < 5; $i++) {
$str = file_get_contents("https://www.googleapis.com/youtube/v3/commentThreads?key=" . "$ytkey" . "&textFormat=plainText&part=snippet&videoId=" . "$url" . "&maxResults=100&nextPagetoken=" . "$nextPage");
$json = json_decode($str, true); // decode the JSON into an associative array
//echo '<pre>' . print_r($json, true) . '</pre>'; // Print json data as array structer ..
echo "$i - " . "Next Page Token : " . $json['nextPageToken']; // Take the next Page Token for get next 100 comment...
echo "<hr>"; // Divider
$nextPage = $json['nextPageToken']; // Take token for next query
// print comments.
foreach ($json['items'] as $val) { // Loop for list comments...
$author = $val['snippet']['topLevelComment']['snippet']['authorDisplayName']; //Get Comment Author Name.
//$author_url = $val['snippet']['topLevelComment']['snippet']['authorChannelUrl']; //Get Comment Author URL.
//$author_thumbnail_url = $val['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; //Get Comment Author Thumbnail URL.
$comment = $val['snippet']['topLevelComment']['snippet']['textDisplay']; //Get Comment Content.
echo "<span style='color:red';>" . "$author" . "</span>" . " --> " . "$comment"; // Author and comment
echo "<hr>"; // Divider
}
}
echo "Process over. ";
?>
I learn how to parse json and how to show them on php from stackoverflow.
Now, there is no problem with taking nextPageTokens. But I can't get comments.
When I run the script, It returns different nextPageToken but comments are same, they come from the first page.
I try to add enough comment line.
Sorry I can't color the php codes.
You are calling commentThreads with the parameter &nextPagetoken=.
The correct parameter to use is &pageToken=.
Here is a recursive,bare bones function to return all comments from a video
public function getAllComments($videoId,$pageToken=null,$maxResults){
$url = "https://www.googleapis.com/youtube/v3/commentThreads";
static $all =[];
$params =[
'key' => 'your-key',
'part' => 'snippet',
'maxResults' => $maxResults,
'videoId' => $videoId,
'pageToken' => $pageToken
];
$call = $url.'?'.http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $call);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$data = NULL;
$data = json_decode($output,true);
$all[] = $data;
if(isset($data['nextPageToken'])){
if($data['nextPageToken'] != NULL ){
$pageToken = $data['nextPageToken'];
getAllComments($videoId,$pageToken,$maxResults);
}
}
curl_close($ch);
return $all;
}
Finally I found a site that does what I want to do.
If you have to collect all comments of a video and take one of them randomly for lottery etc. , use these sites ->
https://www.randomcommentpicker.com
http://commentpicker.com/youtube.html

php foreach loop with 2 variables in json_decode

In a foreach loop i am trying to make a link with data from two variables. Like this:
$baseAttachementUrl = str_replace($rowAttachTags, $rowAttachVals, $getAttachmentURL);
foreach ($attachments as $attachment){
$rowAttachTags = array('{{ROWID}}', '{{SHEETID}}','{{ATTACHMENTID}}');
$rowAttachVals = array($addRowsObj->result[0]->id, $theSheet->id, $attachment->id);
$getAttachmentURL = $baseAttachementUrl;
$getNEWAttachmentURL = str_replace($rowAttachTags, $rowAttachVals, $getAttachmentURL);
$curlSession = curl_init($getNEWAttachmentURL);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
getAttachmentResponse = curl_exec($curlSession);
$getFileObj = array(json_decode($getAttachmentResponse, true));
foreach ($getFileObj as $fileObj){
echo "<a href='". $fileObj['url']."'>". $fileObj['name']."</br></a>";
}
}
The first row in the table works. 2 files comes out with the right url. but in all subsequent rows the last file from the first row comes out.
A couple of problems here that you are not being too specific about. However I will try my best to respond.
First of all, we dont know how the results of $attachments or $getFileObj look like. We are assuming that the part of your question that says "returns stdClas..." is a portion of the results of $getFileObj.
Another problem is that we dont know where you're getting $getAttachmentURL from.. Is this a variable statically set, or is it a variable that comes as a result of $attachments?
Taking those problems into consideration I believe what you're trying to do is to first get the all the attachments and store them into $attachments. Then, you want to get each attachment url from you intial result and iterate through each to get your fileURL. I would do the following:
<?php
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getAttachmentsResponse = curl_exec($curlSession);
// Assign response to variable
$attachments = json_decode($getAttachmentsResponse);
foreach ($attachments as $attachment) {
$getAttachmentURL = $attachment->attachment_URL;
$curlSession = curl_init($getAttachmentURL);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getAttachmentResponse = curl_exec($curlSession);
// Assign response to variable
$getFileObj = json_decode($getAttachmentResponse);
echo "<td>";
echo "<a href='" . $getFileObj->url . "'>" . $attachment->name . "</br></a>";
echo "</td>";
}
Try....
// ... curl setup
$attach = json_decode($getAttachmentsResponse);
// ... curl setup two
$file = json_decode($getAttachmentResponse);
$joinedattachment[ 'attach' ] = $attachone;
$joinedattachment[ 'file' ] = $file;
$attachments[] = $joinedattachment;
$iCountAttachments = count( $attachments );
echo "<td>";
for( $i = 0; $i < $iCountAttachments; ++$i )
{
echo "<a href='". $attachments[ $i ][ 'file' ]."'>". $attachments[ $i ][ 'attach' ] ."</br></a>";
}
echo "</td>";

XML to associative array and echo out specific values

I'm not sure I am going about this the right way but I am trying to echo out individual elements of data from an array, but not succeeding, I only need to grab around 10 variables for average fuel consumption from an XML File here: https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make=honda&model=civic
I only need make, model, year avgMpg which is a child of youMpgVehicle etc so I can place them within a table in the same was as you can echo out SQL data within PHP.
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
//curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$sXML = download_page('https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make=honda&model=civic');
$oXML = new SimpleXMLElement($sXML);
$dom = new DomDocument();
$dom->loadXml($sXML);
$dataElements = $dom->getElementsByTagName('vehicle');
$array = array();
foreach ($dataElements as $element) {
$subarray = array();
foreach ($element->childNodes as $node) {
if (!$node instanceof DomElement) {
continue;
}
$key = $node->tagName;
$value = $node->textContent;
$subarray[$key] = $value;
}
$array[] = $subarray;
// var_dump($array); // returns the array as expected
var_dump($array[0]["barrels08"]); //how can I get this and other variables?
}
The structure is like this: (Or you can click on the hyperlink above)
-<vehicles>
-<vehicle>
<atvType/>
<barrels08>10.283832</barrels08>
<barrelsA08>0.0</barrelsA08>
<charge120>0.0</charge120>
<charge240>0.0</charge240>
<city08>28</city08>
<city08U>28.0743</city08U>
<cityA08>0</cityA08>
<cityA08U>0.0</cityA08U>
<cityCD>0.0</cityCD>
<cityE>0.0</cityE>
<cityUF>0.0</cityUF>
<co2>279</co2>
<co2A>-1</co2A>
<co2TailpipeAGpm>0.0</co2TailpipeAGpm>
<co2TailpipeGpm>279.0</co2TailpipeGpm>
<comb08>32</comb08>
<comb08U>31.9768</comb08U>
<combA08>0</combA08>
<combA08U>0.0</combA08U>
<combE>0.0</combE>
<combinedCD>0.0</combinedCD>
<combinedUF>0.0</combinedUF>
<cylinders>4</cylinders>
<displ>1.8</displ>
<drive>Front-Wheel Drive</drive>
<engId>18</engId>
<eng_dscr/>
<evMotor/>
<feScore>8</feScore>
<fuelCost08>1550</fuelCost08>
<fuelCostA08>0</fuelCostA08>
<fuelType>Regular</fuelType>
<fuelType1/>
<fuelType2/>
<ghgScore>8</ghgScore>
<ghgScoreA>-1</ghgScoreA>
<guzzler/>
<highway08>39</highway08>
<highway08U>38.5216</highway08U>
<highwayA08>0</highwayA08>
<highwayA08U>0.0</highwayA08U>
<highwayCD>0.0</highwayCD>
<highwayE>0.0</highwayE>
<highwayUF>0.0</highwayUF>
<hlv>0</hlv>
<hpv>0</hpv>
<id>33504</id>
<lv2>12</lv2>
<lv4>12</lv4>
<make>Honda</make>
<mfrCode>HNX</mfrCode>
<model>Civic</model>
<mpgData>Y</mpgData>
<phevBlended>false</phevBlended>
<pv2>83</pv2>
<pv4>95</pv4>
<rangeA/>
<rangeCityA>0.0</rangeCityA>
<rangeHwyA>0.0</rangeHwyA>
<trans_dscr/>
<trany>Automatic 5-spd</trany>
<UCity>36.4794</UCity>
<UCityA>0.0</UCityA>
<UHighway>55.5375</UHighway>
<UHighwayA>0.0</UHighwayA>
<VClass>Compact Cars</VClass>
<year>2013</year>
<youSaveSpend>3000</youSaveSpend>
-
33.612226599
45
55
47
28
16
33504
You don't actually need to put everything into an array if you just want to display the data. SimpleXML makes it very simple to handle XML data. If I may suggest a maybe less complex solution:
<?php
function getFuelDataAsXml($make, $model)
{
// In most cases CURL is overkill, unless you need something more complex
$data = file_get_contents("https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make={$make}&model={$model}");
// If we got some data, return it as XML, otherwise return null
return $data ? simplexml_load_string($data) : null;
}
// get the data for a specific make and model
$data = getFuelDataAsXml('honda', 'civic');
// iterate over all vehicle-nodes
foreach($data->vehicle as $vehicleData)
{
echo $vehicleData->barrels08 . '<br />';
echo $vehicleData->yourMpgVehicle->avgMpg . '<br />';
echo '<hr />';
}
To fetch data from an DOM use Xpath:
$url = "https://www.fueleconomy.gov/ws/rest/ympg/shared/vehicles?make=honda&model=civic";
$dom = new DomDocument();
$dom->load($url);
$xpath = new DOMXpath($dom);
foreach ($$xpath->evaluate('/*/vehicle') as $vehicle) {
var_dump(
array(
$xpath->evaluate('string(fuelType)', $vehicle),
$xpath->evaluate('number(fuelCost08)', $vehicle),
$xpath->evaluate('number(barrels08)', $vehicle)
)
);
}
Most Xpath expressions return an a list of nodes that can be iterated using foreach. Using number() or string() will cast the value or content of the first node into a float or string. If the list was empty you will get an empty value.

Counting tweets with a given hashtag using PHP

I am trying to count the number of tweets with a given a hashtag using PHP.
This piece of source code was given by someone on StackOverflow.
Do I need to add any libraries or change any settings for the function to work?
because when I run this code, it just gives me a blank page.
<?php
global $total, $hashtag;
//$hashtag = '#supportvisitbogor2011';
$hashtag = '#australialovesjustin';
$total = 0;
function getTweets($hash_tag, $page) {
global $total, $hashtag;
$url = 'http://search.twitter.com/search.json?q='.urlencode($hash_tag).'&';
$url .= 'page='.$page;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec ($ch);
curl_close ($ch);
//echo "<pre>";
//$json_decode = json_decode($json);
//print_r($json_decode->results);
$json_decode = json_decode($json);
$total += count($json_decode->results);
if($json_decode->next_page){
$temp = explode("&",$json_decode->next_page);
$p = explode("=",$temp[0]);
getTweets($hashtag,$p[1]);
}
}
getTweets($hashtag,1);
echo $total;
?>
Read these articles
https://dev.twitter.com/docs/using-search
https://dev.twitter.com/docs/api/1/get/search
and try with
developer console

Curl For Loop Reading Only Last Element Of Array

Below is the code I am using.
It reads links from a textarea, and then gets the source code and finally filters the meta tags. However it only displays the last element in the array.
So if for example I put 3 websites into the textarea, it will only read the last one, the others are just shown as blank.
Have spent hours trying this, please help.
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
if(isset($_POST['url'])){
$url = $_POST['url'];
$url = explode("\n",$url);
print_r($url);
for($counter = 0; $counter < count($url); $counter++){
$html = file_get_contents_curl($url[$counter]); // PASSING LAST VALUE OF ARRAY
$doc = new DOMDocument();
#$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++){
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
print
('
<fieldset>
<table>
<legend><b>URL: </b>'.$url[$counter].'</legend>
<tr>
<td><b>Title:</b></td><td>'.$title.'</td>
</tr>
<tr>
<td><b>Description:</b></td><td>'.$description.'</td>
</tr>
<tr>
<td><b>Keywords:</b></td><td>'.$keywords.'</td>
</tr>
</table>
</fieldset><br />
');
}
}
This was an annoying little bug to find - but here is the (ridiculously simple) solution:
Your URLs are getting white space added to them, for all but the last URL therefore you'll need to trim it, you can do the following:
curl_setopt($ch, CURLOPT_URL, trim($url));
If available, you could have possibly just used file_get_contents() (still requires you trimming the URL).
The second problem is that if there is no meta data then the old variables are used (from the previous loop) so just before the end of your main loop, after your print() add the following:
unset($title,$description,$keywords);

Categories