I've written a PHP function to get plus ones count for a URL
function makeApiCall($destinationUrl, $stringOfParams){
$curl = curl_init();
echo $destinationUrl.$stringOfParams."<br>";
curl_setopt($curl, CURLOPT_URL, $destinationUrl.$stringOfParams);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
}
While inputting https://plusone.google.com/u/0/_/+1/fastbutton as destination URL and inputting the correct string of params, the result I'm receiving in $result is HTML.
The problem is that I would like using PHP to get the count and not using JavaScript.
How can I do that?
Using preg_match, you can achieve it.
Assuming you are calling a url like that :
https://plusone.google.com/_/+1/fastbutton?bsv=pr&url=http://www.google.com
You are looking for:
<div id="aggregateCount" class="t1">118k</div>
or
<div id="aggregateCount" class="t1">12</div>
So you can perform:
preg_match('/\<div id=\"aggregateCount\" class=\"t1\"\>\>?([0-9]*k?)\<\/div\>/i', $result, $matches);
And $matches will be:
Array
(
[0] => <div id="aggregateCount" class="t1">118k</div>
[1] => 118k
)
edit:
After running the example, it seems that Google return a different number when using curl, for example, on http://www.google.com, it returns:
<div id="aggregateCount" class="t1">>9999</div>
So I've updated the regex to handle the >.
Related
I have a search index on my Cloudant that I query using AngularJS and PHP.
So far I'm not getting specific enough results.
For instance, on a search with fair:'Fair 2017', I'm getting all the results that include Fair, including Fair 2016 and so on.
I've tried different search types (simple, standard, classic), and it happens with all of them.
A typical object:
doc:Object
exhibitortype:"Project Space"
fair:"Fair 2017"
...
Here's my AngularJS code:
$scope.loadexhibitors = function(fair){
$scope.searchindex = fair.doc.fairname;
var $promisefairexh=$http({
url: 'databaseconnect/getexhibitors.php',
method: "GET",
params: {search: $scope.searchindex}
});
...
The PHP bit looks like this:
<?php
$search = $_GET["search"];
$newsearch = str_replace(' ', '+', $search);
$url = "https://user:pass.#user.cloudant.com/db/_design/fairs/_search/by_fair?q='$newsearch'&include_docs=true";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
?>
And my Cloudant search function:
function (doc) {
index("default", doc.fair);
}
On the other hand, on the Cloudant User Interface, when I test the search index and include double quotes on the search input (for example: "Fair 2016" instead of Fair 2016), I get the desired results.
Any tips?
Try using double quotes in your search instead of single quotes, for example:
$url = "https://user:pass.#user.cloudant.com/db/_design/fairs/_search/by_fair?q=\"$newsearch\"&include_docs=true";
Note the change to the q param:
q=\"$newsearch\"
I am trying to update my API with an update curl function but am struggling to work out why it isn't working
The areas where it may be wrong is key($id) I want it to
extract the ID column based on the key value for the ID array.
$URL I want to create the URL based on the const variables plus the resource name plus the value of the ID array that has been passed through rawurlencode.
So far this is my update code, but am wondering what area is wrong.
I can provide more info if needed and appreciate any help, thanks
<?php
function update(array $id,array $vaules, $resourcename)
$jsonData = json_encode($vaules);
key($id);
$url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurlencode("/".$id);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array ('content-type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,PUT);
curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonData);
curl_exec($ch);
curl_getinfo(CURLINFO_HTTP_CODE);
}
The function key() returns the current key in an array (according to the internet pointer). Right now you're not doing anything with it, you're calling the function and not assigning it anywhere.
Did you mean to write: rawurlencode("/".key($id).$vaules);?
As your code is right now, assuming $id is an array, you're trying to convert an array into a string, which I doubt is what you want.
In some cases this works fine, in others like below, its not.
$xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.3a5pre) Gecko/20100526 Firefox/3.7a5pre");
$data = curl_exec($ch);
$ce = curl_error($ch);
curl_close($ch);
// this is how I was doing it prior to today and it worked before
// preg_match_all("/<MealPeriod name=\"(.+?)\">([\w\W\r\n]*?)<\/MealPeriod>/i", $data, $output_array);
// this way doesnt show all the meal periods,
// but I need to know whats in between the MealPeriod tags
// preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array);
// shows all the meal period names,
// but I need the above to work to store whats in between the MealPeriod tags in the $output_array[2]
preg_match_all('/<MealPeriod name="(.*?)">/i', $data, $output_array);
echo '<pre> '.print_r($output_array[1],1).'</pre>';
I tried this on a few regex live sites and 1 of them returned what I needed, while the second did not..
http://www.phpliveregex.com/ -- did work
https://regex101.com/ -- did not work
expected output would by the following for $output_array[1]:
Array
(
[0] => Breakfast
[1] => Every Day
[2] => Outtakes
[3] => Salad Bar
)
But it should also hold whats inbetween the MealPeriod tags in $output_array[2]
Any help would be greatly appreciated
This code below works, all I did was change the regex and change the printing.
The output on screen looks rather odd, because the second (.*?) to capture everything between <MealPeriod> and </MealPeriod> is capturing all the xml tags as well. If you look at the source code, you can clearly see this.
I would encourage you to work with an XML Parser to work with the document. I certainly have used regex to extract portions of XML documents before using a parser to convert them to objects but a parser is much better equipped to work with XML than regex (by leaps and bounds).
Everything is captured, but it is not being printed to the screen with <pre> tags. However, if you look at the source, everything is there.
<?php
$xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.3a5pre) Gecko/20100526 Firefox/3.7a5pre");
$data = curl_exec($ch);
$ce = curl_error($ch);
curl_close($ch);
// this is how I was doing it prior to today and it worked before
// preg_match_all("/<MealPeriod name=\"(.+?)\">([\w\W\r\n]*?)<\/MealPeriod>/i", $data, $output_array);
// this way doesnt show all the meal periods,
// but I need to know whats in between the MealPeriod tags
// preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array);
// shows all the meal period names,
// but I need the above to work to store whats in between the MealPeriod tags in the $output_array[2]
preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array);
echo '<pre> '.print_r($output_array,1).'</pre>';
?>
I found the answer thanks to the following stack overflow post - php regex or | operator
I needed to change the regex to the following and I was finally able to return all the meal periods and contents there of within the correct array.
'/<MealPeriod name="(.*?)">(.*?)<\/?MealPeriod>/i'
hense the ? in <\/?Meal
I want to get information about a channel, is it online at the moment or not:
$stream_list = ...;
$mycurl = curl_init();
curl_setopt ($mycurl, CURLOPT_HEADER, 0);
curl_setopt ($mycurl, CURLOPT_RETURNTRANSFER, 1);
//Build the URL
$url = "http://api.justin.tv/api/stream/list.json?channel=" . $stream_list;
curl_setopt ($mycurl, CURLOPT_URL, $url);
$web_response = curl_exec($mycurl);
but thats always return with an empty array. I saw many example based on it - mine wont work, what am I doing wrong?
An empty array probably means nothing was found with the stream list you provided.
I used http://api.justin.tv/api/stream/list.json?channel=beyondthesummit,towelliee and was able to get an array from the API, and then I used http://api.justin.tv/api/stream/list.json?channel=foobar and got an empty JSON array back.
I'd make sure $stream_list has the value you expect. And if it does, try removing the channel filter completely to see if you get results.
It returns an empty array if the channel is not live.
How can I query a particular website with some fields and get the results to my webpage using php?
let say website xyz.com will give you the name of the city if you give them the zipcode. How can I acehive this easliy in php? any code snap shot will be great.
If I understand what you mean (You want to submit a query to a site and get the result back for processing and such?), you can use cURL.
Here is an example:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
You can grab the Lat/Long from this site with some regexp like this:
if ( preg_match_all( "#<td>\s+-?(\d+\.\d+)\s+</td>#", $output, $coords ) ) {
list( $lat, $long ) = $coords[1];
echo "Latitude: $lat\nLongitude: $long\n";
}
Just put that after the curl_close() function.
That will return something like this (numbers changed):
Latitude: 53.5100
Longitude: 60.2200
You can use file_get_contents (and other similar fopen-class functions) to do this:
$result = file_get_contents("http://other-site.com/query?variable=value");
Do you mean something like:
include 'http://www.google.com?q=myquery'; ? or which fields do you want to get?
Can you be a bit more specific pls :)
If you want to import the html to your page and analyze it, you probably want to use cURL.
You have to have the extensions loaded to your page (it's usually part of PHP _ I think it has to be compiled in? The manual can answer that)
Here is a curl function. Set up your url like
$param='fribby';
$param2='snips';
$url="www.example.com?data=$param&data2=$param2";
function curl_page($url)
{
$response =false;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FAILONERROR,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$page_data=curl_page($url);
Then, you can get data out of the page using the DOM parsing or grep/sed/awk type stuff.