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);
}
Related
I developed the following API in PHP / HTML:
I need the result of the select option to serve input to the following ETL transformation:
I used "RJ" as an example in the query. But I need this result to come from the API. What step can I use to recover this data?
Is the reverse process possible? Can I make the result of this transformation available (which is an excel worksheet) for the user to download?
Edit 1: That's how I get the form results. It's a simple PHP code.
<?php
$conexaobd = new ConexaoBD;
$conexao = $conexaobd->conectarAoBD();
$buscar=$_POST['buscar'];
$sql = "SELECT p.id_projeto, p.municipio, p.estado, p.nome FROM projeto p WHERE p.estado LIKE '%".$buscar."%' ORDER BY p.id_projeto";
$resultado_query = mysqli_query($conexao, $sql);
if (!$resultado_query) {
echo "Erro: " . $sql . "<br>" . mysqli_error($conexao);
}
if (!$linha = $resultado_query->fetch_array(MYSQLI_ASSOC)){
echo "<h3>Nenhum registro encontrado.</h3>";
} else {
while($linha = $resultado_query->fetch_array(MYSQLI_ASSOC)){
$id = $linha['id_projeto'];
$municipio = $linha['municipio'];
$estado = $linha['estado'];
$nome = $linha['nome'];
echo "<h2><strong>ID do projeto: </strong>".#$id."</h2>";
echo "<br/>";
echo "<strong>MunicĂpio: </strong>".#$municipio;
echo "<br/>";
echo "<strong>Estado: </strong>".#$estado;
echo "<br/>";
echo "<strong>Nome do projeto: </strong>".#$nome;
echo "<br/>";
}
}
?>
i have try to use the webservice from some site,
i use nusoap library and my php code like this
<?php
require_once('lib/nusoap.php');
$client = new SoapClient("http://simlitabmas.dikti.go.id/ws_pimnas/ws.svc?wsdl");
$err = $client->getError();
if ($err) {
echo '<h2>errorr BOSSSS </h2><pre>' . $err . '</pre>';
}
$param = "examplekeyword";
$param2 = "001029";
$result = $client->call('get_peserta',array("keyword"=>"$param","kode_perguruan_tinggi"=>"$param2"));
if (!empty($result)) {
echo "<table border=1>";
echo "<tr bgcolor='#cccccc'>";
echo "<th>Name</th>";
echo "<th>Nomor Mahasiswa</th>";
echo "</tr>";
foreach ($result as $item) {
echo "<tr>";
echo "<td>".$item['nama']."</td>";
echo "<td>".$item['nomor_mahasiswa']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
The function get_peserta has two parameter to input.
when i test my php code i got blank page. Any help?
Using the WSDL from http://simlitabmas.dikti.go.id/ws_pimnas/ws.svc?wsdl, you could generate the corresponding package from wsdltophp.com in order to be sure on how to structure your request in PHP as every element will be a PHP object with setters/getters. Let me know if you need any additional help,
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.
I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";
If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>
You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>
This is the code I am trying to use to display information from multiple MySQL tables in one table. I'm also trying to create an else clause if the table information doesn't exist. This is my failed attempt at it.
I keep editing this as I peck away at it but at this point I still can't get it to display the final else when the row doesn't exist.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info WHERE Item_ItemID = $row[ItemID] ";
$info_results = mysql_query($info);
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
}
?>
you could re-write the SQL statement in your $info "SELECT * FROM Info" to "SELECT * FROM Info wHERE Item_ItemID=$row['ItemID'] ... and then delete the
if ($info_row['Item_ItemID'] = $row['ItemID'])
Plus you new a while ind your code after $info_results = mysql_query($info);
* EDITED *
then you do like this, instead of
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
try this
if(mysql_num_rows($info)>0){
while($info_row = mysql_fetch_array($info){
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
}else {
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
i try to edit your code but im not able to try it.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info where Item_ItemID = ".$row['ItemID'];
$info_results = mysql_query($info);
if ($info_row = mysql_fetch_array($info_results))
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
else { //else dispaly Add information to info link.
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
?>
i hope this is the answer.