different number of results using google search api - php

I use google search API to extract the 80th first results. My problem is that each time I run the program, it gives me a different number of results (URLs).
Notice: Trying to get property of non-object on line 42
<?php
$query='site:https://fr.wikipedia.org%20sport';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&hl=fr&rsz=large&q=".$query;
$body = file_get_contents($url);
$json = json_decode($body);
$inF = fopen("fnm.txt","a");
for($x=0;$x<count($json->responseData->results);$x++){
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
fputs($inF,$json->responseData->results[$x]->url); fwrite($inF, "\r\n");
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
$j=0;
for ($i= 0; $i <= 10; $i++)
{
$j=$j+8;
echo $j;
$query = 'site:https://fr.wikipedia.org%20sport';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&hl=fr&rsz=large&start=$j&q=".$query;
$body = file_get_contents($url);
$json = json_decode($body);
for($x=0;$x<count($json->responseData->results);$x++){
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
fputs($inF,$json->responseData->results[$x]->url); fwrite($inF, "\r\n");
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
}
fclose ($inF);

Look at this answer - https://stackoverflow.com/a/4353393/5214904.
In short: there is a limit without API key in 64 results
Edited:
See https://developers.google.com/web-search/docs/#php-access
You need to send your/client IP in url and your site in REFERER field. You can do it with CURL.

Related

I need some assistance with using a specific php function within an HTML webpage

I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";

Google Search with codeigniter

I am developing an application in codeigniter.In my application there is a need to integrate with Google search.In my application there is a textbox and a submit button,If the user searches something it will search the data in google and display the result in my page. How can i do it?Any idea from anyone.
I've successfully used,
<?php
$query = "Steve Jobs";
$api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&&rsz=large&q=".$query;
$body = file_get_contents($api_url);
$json = json_decode($body);
for($x=0;$x<count($json->responseData->results);$x++)
{
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
In Codeigniter you can create a helper function (if to be used many times) for this,
applications/helpers/google_helper.php
<?php
function google_search($query)
{
$api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&&rsz=large&q=".$query;
$body = file_get_contents($api_url);
return json_decode($body);
}

PHP Hyperlinks does not work as variables

I'm a beginner in PHP so may be asking dumb questions. I researched on my question for couple of days prior to bugging you guys. I've two scenarios.
a) MySQL database has 3 fields. description, weblink and header. "weblink" field stores weblinks in the database. Using PHP I'm trying to show weblinks on my webpage upon the user clicking the field "header".Its not working- webpage comes blank with the code below.
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo "<a href = $row['weblink'] > $row['Header']</a>";
echo "<br>";
echo "<hr>";
}
b) Scenario 2: Same example from above but trying to show the links from my database as a button.
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo $row['weblink'];
echo "<br>";
<a href = "$row['weblink']"<button>click me</button></a>;
echo "<hr>";
}
Please help.
Consider the humble printf() instead and apply proper output escaping:
while ($row = mysqli_fetch_array($query)) {
printf('%s<br><br>%s<br><hr>',
htmlspecialchars($row['description'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($row['weblink'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($row['Header'], ENT_QUOTES, 'UTF-8')
);
}
for Scenario 1, You should do something like this (note the braces):
echo "<a href='{$row['weblink']}'>{$row['Header']}</a>";
For scenario 2, you should do something like this (again note braces):
echo "<button>click me</button>";
Remember that Braces work on variables that are within double quotes, and not apostrophes.
Check the below code:
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br><br>";
echo $row['weblink'];
echo "<br>";
echo 'click me';
echo "<hr>";
}
The brackets in the echo are treated as a string not as an array key So use { } these brackets.Also your quotes are not proper. Use the code below
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo "<a href =' {$row['weblink']}' > {$row['Header']}</a>";
echo "<br>";
echo "<hr>";}
Scenario 2
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo $row['weblink'];
echo "<br>";
echo '<button>click me</button>';
echo "<hr>";}
Hope this helps you

Passing URL from MySql via PHP

I'm trying to work out how to pass a URL that's stored in a MySQL database and attach it to a image.
The script i have so far is this:
<?php
$con = mysqli_connect("**********","*********","******","********");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selectedOption = $_POST["mySelect"];
$result = mysqli_query($con,
sprintf("SELECT * FROM `SouthYorkshire` WHERE `EstProv` = '%s'",
preg_replace("/[^A-Za-z ]/", '', $selectedOption)
)
); // pattern based on your html select options
echo "<div id=\"Results\">";
while($row = mysqli_fetch_array($result)) {
echo "<div class=\"ClubName\">";
echo $row['EstName'];
echo "</div><br>";
echo "<div class=\"Location\">";
echo $row['EstAddress2'];
echo "</div>";
echo "<br>";
echo "<div id=\"website\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div>";
}
echo date("Y") . " " ."Search is Powered by PHP.";
echo "</div>";
mysqli_close($con);
the bit i'm trying to change is :
echo "<div id=\"website\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div>"
I tried to use the same above with $row['EstWebsite']
But am not having any success, any suggestions would be great.
Many Thanks
Doesn't matter where the URL comes from, in the end it's just a string of data, and you're just going to be building some HTML with it, so
echo '<div id="website"><img blah blah blah>";
^^^^^^^^^^^^^^^^^^^^^^^-add this ^^^^--add this
You can use the following:
echo "<div id=\"website\"><img src=\"" . $row['ImageColumnName'] ."\" width=\"75\" height=\"25\" /></div>";

PHP Array into mysql select Statement

I have an issue where i have a textarea in a form where users can enter names on seperate lines. On submit I explode the "\n".
I then want to pass the values from the array into an Select statement, but when i run the script it only returns one result (the last one) from the array..
here is the code below.
echo "<h1> You searched for the following names </h1>";
include 'conn.php';
mysql_select_db("email_finder", $con);
$Email = $_POST['EmailBox'];
$str = $Email;
$lines = explode("\n", $str);
//$in = implode(',', $lines);
//$userStr = implode(',', $lines);
echo "<table border='0'>
<tr>
<th style='color:White' width='180px'; bgcolor=#999999>Name</th>
<th style='color:White' width='250px'; bgcolor=#999999>Email</th>
</tr>";
echo "<pre>";
print_r($lines);
echo "</pre>";
foreach($lines as $array_element) {
$result = mysql_query("SELECT * FROM `emails` WHERE `Name` IN('$array_element') ORDER BY `LastName`");
echo "<pre>";
print_r($array_element);
echo "</pre>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "<tr>";
echo "<td style='padding-left:5px'><b> ".$row['Name']."</b>&nbsp:&nbsp</td>";
printf("<td style='padding-left:5px'>" .$row['Email']. "</td>");
echo "</tr>";
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
echo "</table><br />";
echo "Email <b>ALL</b> these Students: <a href=mailto:".$row['Email']." >Click Here</a> <br />";
mysql_close($con);
echo '<br />';
If you can help i would be greatful
Thanks
Try using equals instead of IN:
$result = mysql_query("SELECT * FROM emails WHERE Name='$array_element' ORDER BY LastName");
Otherwise my advise to debug it would be to put an exit(); statement on your first pass of the while loop to check your array value and/or if you get a result of the first entry. something like:
foreach($lines as $array_element) {
$result = mysql_query("SELECT * FROM `emails` WHERE `Name`='$array_element' ORDER BY `LastName`");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
print_r($row);
exit;
...
Guessing you've got OS specific line endings tripping you up. Try preg_split
$lines = preg_split("/\\n|\\r|\\r\\n/", $str);

Categories