How to perform a search query using Services_Twitter? - php

I am trying to perform a Twitter search using the PEAR package Services_Twitter.
Unfortunately this only returns an array of status ids, for example (var_dump):
object(stdClass)#88 (2) {
["statuses"]=>
array(11) {
[0]=>
int(49497593539)
[1]=>
int(49497593851)
[2]=>
int(49497598001)
[3]=>
int(49497599055)
[4]=>
int(49497599597)
[5]=>
int(49497600733)
[6]=>
int(49497602607)
[7]=>
int(49497607031)
[8]=>
int(49497607453)
[9]=>
int(49497609577)
[10]=>
int(49497610605)
}
["created_in"]=>
float(0.008847)
}
The script I'm using is similar to this test script I wrote:
<?php
//$oAuth = new HTTP_OAuth_Consumer( /** Supply oAuth details here **/ );
$Twitter = new Services_Twitter();
//$Twitter->setOAuth($oAuth);
try {
$Response = $Twitter->search(array(
"q" => "#FF -RT OR #FollowFriday -RT",
"rpp" => 10,
"since_id" => 23982086000,
"result_type" => "recent"
));
var_dump($Response);
} catch (Exception $e) {
fwrite(STDERR, $e->getMessage());
}
?>
Since I want to scan the tweets for certain words and want to know when it was posted and by whom, I would need to request all these statuses one by one.
But according to the example response in the Twitter API documentation they already return all the necessary information about the tweets (which is kinda obvious).
So, the question is: How can I access this information using Services_Twitter?
Kind Regards,
Arno

So as I said ->search() is wrapped through Services_Twitter::__call().
But here's the mis-understanding!
Two searches:
http://api.twitter.com/1/search.json?q=#noradio
http://search.twitter.com/search.json?q=#noradio
This is confusing as search.twitter.com returns the results as you'd expect them and the other API method just the status IDs.
For some reason only when you search for trends search.twitter.com is used. Otherwise it's the API methods. If you want to help, please open a ticket on PEAR and I can try to implement this for you.
A quickfix for you is this script:
<?php
$uri = 'http://search.twitter.com/search.json?';
$uri .= http_build_query(
array(
"q" => "#FF -RT OR #FollowFriday -RT",
"rpp" => 10,
"since_id" => 23982086000,
"result_type" => "recent"
));
$response = file_get_contents($uri);
if ($response === false) {
fwrite(STDERR, "Could not fetch search result.");
exit(1);
}
$data = json_decode($response);
var_dump($data);

Are you using a custom Services_Twitter, I just did a search through the class via Pear Documentation and was unable to find the search function. However, it seems like most of the returns for that class is a simple_xml object. Given that I would look through the documentation there and see how you can pull that data out. It would also help looking at how Twitter returns the response in XML format.

Related

Stripe API: Filter Paid and Unpaid Invoices

I'm new to using the Stripe API and I'm running into trouble trying to get a list of unpaid and paid invoices from a particular customer.
To preface using an example: I have a customer, they have 1 unpaid invoice and 0 paid invoices. There have been 19 failed payment attempts and none that were successful. I want to show a list of paid invoices (which there should be 0) and unpaid (which there should be 1).
When I retrieve invoices using the following code:
$response = \Stripe\Invoice::all(array("customer" => $cust, "paid" => false));
It returns object(Stripe\Collection)#7 (7) { ["_requestParams":protected]=> array(2) { ["customer"]=> string(18) "CUST_ID" ["paid"]=> bool(false) } ...
It's showing one record which is what I want but I don't believe it's retrieving the unpaid invoice.
Now if I try to retrieve invoices using the following:
$response = \Stripe\Invoice::all(array("customer" => $cust, "paid" => true));
I get an entire page of objects the first line of which being: object(Stripe\Collection)#7 (7) { ["_requestParams":protected]=> array(2) { ["customer"]=> string(18) "CUST_ID" ["paid"]=> bool(true) }...
With the ["paid"] => bool(true) line appearing 10 times.
I've been looking at the Stripe API documentation but it doesn't really seem to show examples of what I'm looking for.
Any help would be greatly appreciated :)
I've filtered invoice using below arguments
$response = \Stripe\Invoice::all(array("customer" => $cust, "billing" => charge_automatically));
Ex.
billing => charge_automatically
billing:
The billing mode of the invoice to retrieve. Either charge_automatically or send_invoice.
For more details, https://stripe.com/docs/api#list_invoices
Thank you.

Why are the properties of Google_Service_Drive files only returning null?

I want to list all files in a folder shared with my Service Account. I'm trying to figure out what files have a 'parent' attribute set to the folder ID or folder name. However, all the objects that I output show "NULL" for nearly all fields. I've searched through the Google Drive API v3 and I can't even find the listFiles() function in it. Additionally, the documentation recommends using the Files.list function for outputting a list of files but when I use the Files.list function, I get an error as if the method doesn't exist. So I went back to using listFiles()... My code looks like this:
public function getTitlesAndIDs($folderID)
{
// $capabilities = new \Google_Service_Drive_DriveFileCapabilities();
// $capabilities->canListChildren = true;
$files = $this->driveService->files->listFiles();
var_dump($files);
}
Trying to add capabilities just returns NULL as well. When I run listFiles() I get output like this:
["files"]=>
array(100) {
[0]=>
object(Google_Service_Drive_DriveFile)#77 (65) {
["collection_key":protected]=>
string(6) "spaces"
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
NULL
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
...
Why are the values all coming back as NULL and how do I fix this so I can search by parent folders? Is there a php equivalent to the Files.list function?
Thanks in advance.
First you need to make sure that there are files that the service account has access to. Once you have done so this should be able to list the files.
$service = new Google_Service_Drive($client);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
}
}
Code ripped from PHP Quickstart
In addition to what #DalmTo said, one important thing to explicitly point out: you received NULL for nearly all fields because you didn't supply a list of fields, 'fields' as optional parameter.
In addition to Google Drive API V3 properties such as id, name, kind and description, there are custom file attributes available to the developer via properties and appProperties.
https://developers.google.com/drive/api/v3/properties
Google Drive properties key / value pairs are controlled and manipulated by your application.
Throughout Google Drive V3 documentation, the term "properties" also refers to metadata. I found this very confusing. https://developers.google.com/drive/api/v3/reference/files
In Google Drive V2, there are copious examples of how to manipulate key / value properties using PHP, but in Google Drive V3 the properties API was significantly changed, and PHP code examples are very sparse.
By modifying the example code above, I was able to retrieve my customized properties.
$service = new Google_Service_Drive($client);
// Print the names, IDs and properties for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name, properties)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", "name: ".$file->getName(), "ID: ".$file->getId());
foreach ($file->getProperties() as $key => $value) {
printf("%s%s\n","-key: ".$key,", value: ".$value);
}
}
}
I wanted to share my extension of the original answer for those developers who land on this page looking for a PHP example of manipulating properties and appProperties in Google Drive V3.

Google Analytics PHP API : goals not found

In my company we decided to use Google Analytics to get some intersting metrics about visitors, entrance channels etc ...
I created a goal which is "triggered" when a visitor submit the contact form, everything work perfectly well and I even created a segment to preview the difference between people who use the form and the others.
Using the PHP API, i have my own dashboard table giving me some details about each session, one by one, with :
every visited URL
date & time
if come from Adwords, then the keywords
Below a screenshot from my dashboard, with a visitor who went to my website thought the homepage (first row), then submit the contact form (second row).
My own datas table from GA PHP API
But.. i do not manage to link those datas with my goal !
I tried metrics like :
ga:goal1start
ga:goal1Completions
ga:goal1Value
ga:goal1ConversationRate
(https://developers.google.com/analytics/devguides/reporting/core/dimsmets)
Whatever the metric I test (last column in my screnshot), the metric = 0 but I see completed goals in my Google Analytics dashboard for some of those sessions.
Is there something wrong ?
Thanks for your help ;)
EDIT 01/10 - Below is my PHP code :
$optParams = array(
'dimensions' => 'ga:dimension3, ga:pagePath, ga:date, ga:adMatchedQuery, ga:hour',
'sort' => '-ga:date, -ga:hour',
//'filters' => 'ga:medium==organic',
'max-results' => '99999');
return $analytics->data_ga->get(
'ga:' . $profileId,
'30daysAgo',
'today',
'ga:pageviews, ga:goal2Completions ',
$optParams);
And this is one row from the result :
array(7) {
[0]=>
string(12) "John Cena" // ga:dimension3
[1]=>
string(32) "/financial-expertise.php" // ga:pagePath
[2]=>
string(8) "20170110" // ga:date
[3]=>
string(9) "(not set)" // ga:adMatchedQuery
[4]=>
string(2) "12" // ga:hour
[5]=>
string(1) "1" // ga:pageviews
[6]=>
string(1) "0" // ga:goal2Completions ??
}
As you would see, the key "6" is related to the "ga:goal2Completions" metrics and in the above array, I guess it should be at least "1" ?
for the php code
can you try this one and tell me if its work or not ==>
$optParams = array(
'dimensions' => 'ga:dimension3, ga:pagePath, ga:date, ga:adMatchedQuery, ga:hour',
'sort' => '-ga:date, -ga:hour',
//'filters' => 'ga:medium==organic',
$metrics = 'ga:pageviews, ga:goal2Completions';
$from = date('Y-m-d', strtotime("last day of -1 month"));
$to = date('Y-m-d', strtotime("today"));
try {
$campaigns = $service->data_ga->get( 'ga:' . $profileId,
$from,
$to ,
$metrics,
$optParams);
} catch (\Google_Exception $e) {
$message = sprintf("<h3>Oups !</h3>
<p>Request to analytics failed.
Google error message was :</p>
<p><q>%s</q></p>", htmlspecialchars( $e->getMessage() ));
you need to { print_r } $compaigns and see the results
OK after hours of research .. no solution. So I have edited my goal to be equal to an event and then change my report to extract event. That works. Thx for your help!

Get the from and to object from p2p_id in WordPress - WP Posts to Posts Plugin

I'm now using WP Posts to Posts plugin in wordpress.
And now I made some links between posts and posts, users and users, or posts and users.
And in all three cases above, I want to do more with the p2p plugin.
For example, I may fetch the p2p_id first:
$users = get_users( array(
'connected_type' => 'multiple_authors',
'connected_items' => $post
) );
foreach($users as $user) {
$p2p_id = $user->p2p_id;
// ********** ATTENTION *********
// Here, I got the p2p_id of the p2p object
// In a general purpose, I want to get the
// from and to object from the p2p_id
}
So, how can I get the from and to object via the p2p_id? I've found over the documentation, but seemed no effective ways.
No other answers. So I finally found the answer myself from the source code:
File: /wp-content/plugins/posts-to-posts/vender/scribu/lib-posts-to-posts/api.php
Notice that there is a function: p2p_get_connection($p2p_id)
Then we call that function with a known p2p_id, an stdClass Object is returned, like the below:
object(stdClass)#2509 (4) {
["p2p_id"]=>
string(1) "8"
["p2p_from"]=>
string(2) "84"
["p2p_to"]=>
string(1) "2"
["p2p_type"]=>
string(10) "my_post_to_user"
}
Now that we can get the p2p_from id and p2p_to id, and we know it is a post or an user. We can construct the object.
So the finally solution may look like:
$conn = p2p_get_connection($this->p2p_id);
$from = get_post(intval($conn->p2p_from));
$to = new WP_User(intval($conn->p2p_to));
Still seemed not found in the documentation, hope it helps.

Creating json array for Jira multi-checkbox with PHP

I'm receiving data in the following format from a multi-checkbox
["Ethnicity"]=>
array(3) {
["Maori"]=>
string(5) "Maori"
["Pacific Peoples"]=>
string(15) "Pacific Peoples"
["Other European"]=>
string(14) "Other European"
}
I'm trying to get this into a multi-checkbox in Jira via an API call using the following segment
'customfield_11337' => [
"value" => $data["Ethnicity"]
],
But this returns an error string(21) "data was not an array"
So I've tried to massage the data into a single array using
$ethnicityArray = array();
foreach ($data["Ethnicity"] as $eth => $value) {
array_push($ethnicityArray, $value);
}
But this returns the same error. I should note that I've got no problem populating radio buttons, text fields etc in Jira via the same method. It just seems to be checkboxes I can't get right.
How do I go about solving this using PHP?
The correct solution was to get the data into a format that looks like:
'customfield_11333' => [["value" => "Asian"], ["value" => "Other"]]

Categories