Google web search api to get number of results with php - php

for keyword selection (seo)I need to know only the number of results for specific group of keywords. got this code from google https://developers.google.com/web-search/docs/), but no way. PHP version is 5.6
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=relax&userip=MYIP";
$referrer = "http://localhost:8080/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referrer);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
in this case ... no results at all
I tried other ways ... eg.:
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=relax";
$body = file_get_contents($url);
$json = json_decode($body);
$results= $json->responseData->cursor->resultCount;
in this case I got some schizophrenic results (sometimes I got numbers, sometimes I got nothing)... in any case always under the declared limit of 1000.
any suggestion?
Thanks in advance.

Related

Zoho Get an Invoice

I am trying to integrate zoho with my web app. I am making an api call to get a particular invoice. The call as per documentation is
'https://books.zoho.com/api/v3/invoices/INV_ID?organization_id=XXXXXXXX'
This works fine as well. But when I set a variable for INVOICE ID, it returns all the invoices. (200 INVOICES) The call is below.
'https://books.zoho.com/api/v3/invoices/' .$inv_id. '?organization_id=XXXXXXXX';
where I am making the mistake? The method is below.
$zid = '';
if(isset($_POST['_zid'])){
$zid = $_POST['_zid'];
}
get_invoice($access_token, $zid);
function get_invoice($access_token, $zid){
$invid = $zid;
$service_url = $GLOBALS['service_url'] . 'https://books.zoho.com/api/v3/invoices/' .$invid. '?organization_id=XXXXXXXXX';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Zoho-oauthtoken '. $access_token,
'Content-Type: "application/json"'));
$res = curl_exec($ch);
curl_close($ch);
$dec_res = array();
$dec_res = json_decode($res, true);
print_r($dec_res);
}
Everything is fine except a small thing. I didn't think of Array Declaration. The working call is straightly given an integer as the invoice_id. when I pass the variable declared as a string. so although the digits it is counted as a string which makes the call invalid.
//THIS WILL DO THE TRICK
$zid = 0;
if(isset($_POST['_zid'])){
$zid = $_POST['_zid'];
}

Firebase database delete specific data by php

How can I remove firebase specific data? I use the
php Kreait\Firebase library.
$fg = $database->getReference('raw_check_out')->orderByChild('reciptno')->equalTo($recipt)->getSnapshot();
$reb = $fg->getValue();
$fg->remove();
but this is not working.
Based on your code example:
$fg = $database
->getReference('raw_check_out')
->orderByChild('reciptno')
->equalTo($recipt)
->getSnapshot();
Here $fg does not hold the reference, but the snapshot.
If you want to remove the reference after you have retrieved the data you need, you need the reference itself:
$fg = $database->getReference('raw_check_out');
$query = $fg->orderByChild('reciptno')->equalTo($recipt);
$reb = $query->getSnapshot()->getValue();
$fg->remove();
This function is for unsubscribing users if you want to remove "user-id-8776" and your structure is like:
public function unsubscribe($uid)
{
$ref = $this->database->getReference()->getChild('users')->getChild($uid)->orderByChild($uid)->getReference();
$path = $ref->getUri()->getPath();
$ref->remove();
}
For any other data is more or less the same, you just have to find the reference of data you want to delete it and then you can remove it.
Check Image
You have another option to resolve this problem. This solution given below:
$url = "https://<projectID>.firebaseio.com/chatList/".<jsonFileName>."json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);

Does anyone have any scripts to share that retrieve products using Commission Junction's API that actually work?

This example only displayed a blank page for me.
This one did as well.
I've got the latest version of PHP and cURL set up properly, as far as I know so there shouldn't be any problem at that end. I'd prefer JavaScript to retrieve products but I'm open minded.
I happen to not be highly skilled, but I'd like to get my foot in the door.
edit: I will show you the code that doesn't work, and the error it is giving me.
<?php
// Your developer key
$cj_id = "My ID - omitted for privacy.";
// Your website ID
$website_id = "Also removed for privacy.";
// Keywords to search for
$keywords = "credit+card";
// URL to query with cURL
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
// Initiate the cURL fetch
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send authorization header with the CJ ID. Without this, the query won't work
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$cj_id));
$result = curl_exec($ch);
// Put the results to an object
$resultXML = simplexml_load_string($result);
// Print the results
print "<pre>";
print_r($resultXML);
print "</pre>";
?>
Now, this is the error that it's giving me.
SimpleXMLElement Object
(
[error-message] => Invalid Key provided. Valid keys are: advertiser-ids, advertiser-sku, currency, high-price, high-sale-price, isbn, keywords, low-price, low-sale-price, manufacturer-name, manufacturer-sku, page-number, records-per-page, serviceable-area, sort-by, sort-order, upc, website-id
)
You have a error in your URL, try this:
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
instead of :
$url = "https://product-search.api.cj.com/v2/product-search?website-id=$website_id&keywords=$keywords";
<?php
echo '<pre>';
$url='https://product-search.api.cj.com/v2/product-search?website-id=your-id-key-here&advertiser-ids=4415206&records-per-page=999&serviceable-area=US';
$CJ_KEY='0085eb59c8928f028ba5b27bccfe17cdd20cf4e9079b977b2cc6df72752abab9205676a2f7ee67befe9dccab85f656ef46aba49e500faccbf75dfc6e03f655334d/00848a3f9bf0e13525bce27f008d6245c3e42ae80f2d80a8d9d2220807ca386f4b10146cbbcfff06aafb5e49c03a3318213389dee7861abb2dd7229470390a89c9';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, FAlSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CJ_KEY));
$curl_results = curl_exec($ch);
$xml = simplexml_load_string($curl_results);
var_dump($xml);
// Loop Insert Product to database
echo '<pre>';
// if you no set: records-per-page=999, default get 50 products latest
// advertiser-ids=4415206 is Id of Advertiser in CJ, you can replace other id ,
Hope helpful for you , good luck !
?>

Get more than 10 results by google search API in php

I am trying to get 10 pages result listed using the following cod below. When i run the URL directly i get a json string but using this in code it does not returns anything. Please tell me where i am doing wrong.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=CompTIA A+ Complete Study Guide Authorized Courseware site:.edu&start=20";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
print_r($json);
Now i am using the following code but it outputs only four entries of a page. Please tell me where i am doing wrong.
$term = "CompTIA A+ Training Kit Microsoft Press Training Kit";
for($i=0;$i<=90;$i+=10)
{
$term = $val.' site:.edu';
$query = urlencode($term);
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start='.$i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
//print_r($json);
foreach($json['responseData']['results'] as $data)
{
echo '<tr><td>'.$i.'</td><td>'.$url.'</td><td>'.$k.'</td><td>'.$val.'</td><td>'.$data['visibleUrl'].'</td><td>'.$data['unescapedUrl'].'</td><td>'.$data['content'].'</td></tr>';
}
}
Just try with urlencode
$query = urlencode('CompTIA A+ Complete Study Guide Authorized Courseware site:.edu');
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start=20';

Using the GoogleAPI to return the number of search results for 2 different queries. (PHP)

I'm working on a small experiment for my Honours Project. What I'm trying to achieve is to compute the similarity between the amount of results returned, based on 2 Google search queries. When I have the 2 results, I wish to divide one by the other in order to return a 'ratio' of sorts.
Now, it's easy enough to return one result using the API, I've been using this code:
$query="CinammonRolls";
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$query";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
print 'The results for ' . $query . ' are: ' . $json->responseData->cursor->estimatedResultCount;
That piece of code, returns the result fine. However I'm relatively new to PHP so the follow code, wasn't ever going to work; but I gave it a bash.
$query="CinammonRolls";
$query2="CinammonRollsChinese";
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$query";
$url2 = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$query2";
$ch = curl_init();
$ch2 = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 2);
$body = curl_exec($ch);
$body2 = curl_exec($ch2);
curl_close($ch);
curl_close($ch2);
$json = json_decode($body);
$json2 = json_decode($body2);
print 'The results for ' . $query . ' are: ' . $json->responseData->cursor->estimatedResultCount;
print 'The results for ' . $query2 . ' are: ' . $json2->responseData->cursor->estimatedResultCount;
That just returns the error:
Notice: Undefined property: stdClass::$estimatedResultCount in C:\Program Files\EasyPHP-5.3.9\www\googleTest.php on line 24
So I emplore you all, is there a way to get round this? Would it be something like computing both the results in separate functions? I'm stumped.
Thanks for any help.
I think you meant:
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 2);
to be:
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
The option is to be turned on and off based on the 3rd parameter which is boolean, so 0 or 1. I think when you copied and pasted the lines for the 2nd query and was changing 1 to 2 you accidentally changed the 1 boolean to 2 (non-boolean).
"CinammonRollsChinese" returns NO RESULTS thus there is not estimated result count. You need to check to see if $json2->responseData->cursor->estimatedResultCount exists before doing anything with it since not every search string will yield results.
Avoid code repetition by extracting the querying code to a method, or to a class it it gets more complicated. See the rewrite below:
function print_google_query_results($query) {
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$query";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
print 'The results for ' . $query . ' are: ' . $json->responseData->cursor->estimatedResultCount;
}
$query="CinammonRolls";
$query2="CinammonRollsChinese";
print_google_query_results($query);
print_google_query_results($query2);
Thanks to everyone for the help, I really appreciate it. It's working exactly as I hoped now (with much cleaner code too!). Even though the main problem was the cause of me 'having the dumb'! Thanks for pointing that out, I'd be stuck with this till midnight. The problem was that a) My code was pretty lousy and could be compiled much better. and b) I didn't check to see if my second query 'CinammonRollsChinese' actually returned any results. It didn't.
Here is the final working code.
$query="CinammonRolls";
$query2="DeliciousCake";
print_google_query_results($query);
print '<br>';
print_google_query_results($query2);
function print_google_query_results($query) {
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$query";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
print 'The results for ' . $query . ' are: ' . $json->responseData->cursor->estimatedResultCount;
}
Superb. Huge thanks again to everyone.

Categories