How can I get tweets from new twitter api 1.1? - php

I have just implemented https://github.com/thujohn/twitter-l4 for Laravel 4, it basically pulls in an array
of tweets from a search of a hashtag.
It seems like its working, I think there are some things that are not working though.
I get a blank screen with no errors which means a positive sign.
Here is my code.
PHP:
$tweets = Twitter::getSearch(array('q' => 'secretsocial', 'count' => 100, 'format' => 'array'));
var_dump($tweets);
This code basically gives me an array of json:
array (size=2)
'statuses' =>
array (size=20)
0 =>
array (size=24)
'metadata' =>
array (size=2)
'result_type' => string 'recent' (length=6)
'iso_language_code' => string 'en' (length=2)
'created_at' => string 'Fri May 16 14:28:14 +0000 2014' (length=30)
'id' => int 467310562603331586
'id_str' => string '467310562603331586' (length=18)
'text' => string 'On that #merch #grind. #spotify #swag just got shipped in. #secretsocial #free #leeds #leedsuni… http://t.co/67phqjeg5W' (length=121)
'source' => string 'Instagram' (length=59)
'truncated' => boolean false
'in_reply_to_status_id' => null
'in_reply_to_status_id_str' => null
'in_reply_to_user_id' => null
'in_reply_to_user_id_str' => null
'in_reply_to_screen_name' => null
'user' =>
array (size=40)
'id' => int 167993141
'id_str' => string '167993141' (length=9)
'name' => string 'Maral Erol' (length=10)
'screen_name' => string 'liaaca' (length=6)
'location' => string 'Leeds / San Diego' (length=17)
'description' => string 'Music/Culture/Entertainment Enthusiast.' (length=39)
'url' => string 'http://t.co/FL3uuA6QcN' (length=22)
'entities' =>
array (size=2)
'url' =>
array (size=1)
'urls' =>
array (size=1)
0 =>
array (size=4)
'url' => string 'http://t.co/FL3uuA6QcN' (length=22)
'expanded_url' => string 'http://linkd.in/R70tjB' (length=22)
'display_url' => string 'linkd.in/R70tjB' (length=15)
'indices' =>
array (size=2)
0 => int 0
1 => int 22
'description' =>
array (size=1)
'urls' =>
array (size=0)
empty
So from that I wrote this:
if(isset($tweets->statuses) && is_array($tweets->statuses)) {
if(count($tweets->statuses)) {
foreach($tweets->statuses as $tweet) {
echo $tweet->text;
}
}
else {
echo 'The result is empty';
}
}
I get no errors on the page. Can anyone point me in the right direction please?
Cheers

Since each $tweet is an array so you should use $tweet['text'] instead of $tweet->text
foreach($tweets->statuses as $tweet) {
echo $tweet['text'];
}
Also $tweets->statuses should be $tweets['statuses'].

Related

PHP. array_column() analogue for SimpleXMLElement object

object(SimpleXMLElement)[803]
public 'row' =>
array (size=13)
0 =>
object(SimpleXMLElement)[797]
public '#attributes' =>
array (size=4)
'codeonimage' => string '01' (length=2)
'name' => string 'Крышка' (length=12)
'oem' => string '13711251885' (length=11)
'ssd' => string '$HgsQRnNPF0d$' (length=174)
public 'attribute' =>
array (size=2)
0 =>
object(SimpleXMLElement)[813]
public '#attributes' =>
array (size=3)
'key' => string 'amount' (length=6)
'name' => string 'Количество' (length=20)
'value' => string '1' (length=1)
1 =>
object(SimpleXMLElement)[814]
public '#attributes' =>
array (size=3)
'key' => string 'end_of_production' (length=17)
'name' => string 'end_of_production' (length=17)
'value' => string 'Не производтся с: 19871116' (length=40)
...
I'm trying to take all the 'oem' values from each SimpleXML element like:
array_column($simpleXMLObject->row, 'oem');
... and of course I get an error:
array_column() expects parameter 1 to be array, object given
There is another option with a full search. But maybe there is some more pretty way to do this?

multidimensional array unset not working

Here is my array;
var_dump($contact['poco']['tags']);
array (size=5)
0 =>
array (size=3)
'tag' => string 'boy' (length=3)
'color' => string '#332409' (length=7)
'id' => string '57160583b0e6df19598b4568' (length=24)
1 =>
array (size=3)
'tag' => string 'girl' (length=4)
'color' => string '#2e2f15' (length=7)
'id' => string '57160589b0e6df1d598b4567' (length=24)
2 =>
array (size=3)
'tag' => string 'zebra' (length=5)
'color' => string '#646604' (length=7)
'id' => string '57160592b0e6df7b588b4567' (length=24)
3 =>
array (size=3)
'tag' => string 'potential duplicate' (length=19)
'color' => string '#f00' (length=4)
'id' => string '57161d9db0e6df0f5c8b456b' (length=24)
4 =>
array (size=3)
'tag' => string 'no phone numbers' (length=16)
'color' => string '#5833d2' (length=7)
'id' => string '5716059ab0e6df7b588b456d' (length=24)
I just want to unset/remove one that have the following tags;
$smartTags = ['potential duplicate', 'no emails', 'no phone numbers'];
So I end up with;
array (size=3)
0 =>
array (size=3)
'tag' => string 'boy' (length=3)
'color' => string '#332409' (length=7)
'id' => string '57160583b0e6df19598b4568' (length=24)
1 =>
array (size=3)
'tag' => string 'girl' (length=4)
'color' => string '#2e2f15' (length=7)
'id' => string '57160589b0e6df1d598b4567' (length=24)
2 =>
array (size=3)
'tag' => string 'zebra' (length=5)
'color' => string '#646604' (length=7)
'id' => string '57160592b0e6df7b588b4567' (length=24)
I have tried;
$smartTags = ['potential duplicate', 'no emails', 'no phone numbers'];
foreach ($contact['poco']['tags'] as $key => $tag) {
if (in_array($tag, $smartTags)) {
unset($contact['poco']['tags'][$key]);
}
}
But it doesn't do anything. I might be having trouble because of the multi-dimensionalness of this array...
What is the correct syntax?
Try up with this.
foreach ($contact['poco']['tags'] as $key => $tag) {
if (in_array($tag['tag'], $smartTags)) {
unset($contact['poco']['tags'][$key]);
}
}

How to write a find query with condition in php-mongodb?

I'm having problem with find query in php-monogodb. find() without conditions works fine but when using condition inside find() method it won't give any results. How to solve this issue?
$connection = new Mongo();
$db = $connection->selectDB('db1');
$collection = $db->selectCollection('customers');
$cursor = $collection->find(array('CUSTOMER_ID' => $id));
$num_docs = $cursor->count();
if($num_docs > 0)
{
foreach($cursor as $obj)
{
echo 'Customer-Id: '.$obj['CUSTOMER_ID']."\n";
echo 'Customer Name: '.$obj['CUST_FIRST_NAME']." ".$obj['CUST_LAST_NAME']."\n";
echo 'Customer Email: '.$obj['CUST_EMAIL']."\n";
echo "\n\n\n";
}
}
After var_dump($cursor->explain()); it prints:
array (size=3)
'queryPlanner' =>
array (size=6)
'plannerVersion' => int 1
'namespace' => string 'db1.customers' (length=13)
'indexFilterSet' => boolean false
'parsedQuery' =>
array (size=1)
'CUSTOMER_ID' =>
array (size=1)
...
'winningPlan' =>
array (size=3)
'stage' => string 'COLLSCAN' (length=8)
'filter' =>
array (size=1)
...
'direction' => string 'forward' (length=7)
'rejectedPlans' =>
array (size=0)
empty
'executionStats' =>
array (size=7)
'executionSuccess' => boolean true
'nReturned' => int 0
'executionTimeMillis' => int 1
'totalKeysExamined' => int 0
'totalDocsExamined' => int 325
'executionStages' =>
array (size=14)
'stage' => string 'COLLSCAN' (length=8)
'filter' =>
array (size=1)
...
'nReturned' => int 0
'executionTimeMillisEstimate' => int 0
'works' => int 327
'advanced' => int 0
'needTime' => int 326
'needYield' => int 0
'saveState' => int 2
'restoreState' => int 2
'isEOF' => int 1
'invalidates' => int 0
'direction' => string 'forward' (length=7)
'docsExamined' => int 325
'allPlansExecution' =>
array (size=0)
empty
'serverInfo' =>
array (size=4)
'host' => string 'deadpool' (length=8)
'port' => int 27017
'version' => string '3.2.4' (length=5)
'gitVersion' => string 'e2ee9ffcf9f5a94fad76802e28cc978718bb7a30' (length=40)
Can you explain what is going wrong?
$cursor = $collection->find(array('CUSTOMER_ID' => $id)); where are you getting the id value from.
and try not using id as sometimes it has issues finding the value esp if $id is used elsewhere.

Symfony2 Doctrine Array of joined objects

I have the following repository:
class CustomerRepository extends EntityRepository
{
public function searchCustomer($criteria)
{
$q = $this->createQueryBuilder('c');
$q->join('TeamERPCustomerBundle:Company', 'o',
'WITH', 'c.company = o.id');
if (isset($criteria) and $criteria!=""){
$q->orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
(...)
}
$q = $q->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
return $q;
}
}
I call it from the controller as follows:
$result = $this->getDoctrine()->getManager()
->getRepository('TeamERPCustomerBundle:Customer')
->searchCustomer($customerInfo);
It returns an array of elements as follows:
array (size=20)
0 =>
array (size=9)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
1 =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
2 =>
array (size=9)
'id' => int 2
'customer_name' => string 'Kay' (length=3)
'address' => null
'postal_address' => null
'city_town_village' => null
'e_mail' => null
'land_line' => null
'cell_phone' => null
'fax' => null
3 =>
array (size=2)
'id' => int 3
'company_name' => string 'DTC' (length=3)
(...)
It gives one element of the array per object, but what I want it the classic mysql join; as result an array of elements of the same type. Something like mergin elements 1 and 2 together.
The other problem with this query is that is does not work like that classic join ether, it gives all the customers, but only the companies that are not repeated.
Can anyone help me to get a uniform array of elements?
Edit: I managed to find a solution:
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT c, i
FROM TeamERPCustomerBundle:Customer c
JOIN c.company i');
//$query->setParameter('id', '1'); With this is can add as many parameter as I need
return $query->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
This returned something like this:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
As you might have already appreciated this array is different from what I was getting before. so now I just have to go through the array and change it to what ever I want. for instance:
foreach ($result as $key => $value){
$result[$key]['company'] = $value['company']['company_name'];
}
Now we are talking. This Is kind of what I wanted:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' => string 'Debswana' (length=8)

php multidimensional array sort

I have this multidimensional array
I am wondered how can i sort this array again so i can use it in for loop.
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
3 =>
array (size=1)
3 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
How can I reindex this array to become
array (size=3)
0 =>
array (size=1)
0 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721708
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'a' (length=1)
'notify' => string '0' (length=1)
1 =>
array (size=1)
1 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721711
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'c' (length=1)
'notify' => string '0' (length=1)
2 =>
array (size=1)
2 =>
array (size=7)
'username' => string 'wajdi' (length=5)
'userimage' => string 'file_3898.jpg' (length=13)
'date' => int 1373721712
'postid' => string '118' (length=3)
'type' => string 'comment' (length=7)
'comment' => string 'd' (length=1)
'notify' => string '0' (length=1)
I tried array_shift and array_chunk but nothing works !!!
Please help, thank you all :)
Use array_multisort to sort multi-dimensional arrays or to sort an array using multiple keys.
I think this should do it but it's be a lot cleaner if you didn't have the extra level off the array.
$new_array = array();
$index = 0;
foreach($array as $i1 => $a1){
foreach($a1 as $i2 => $a2){
$new_array[$index][$index] = $a2;
}
$index++;
}
You can use 'array_values' for re-indexing which start index from 0. As per your requirement inner array are not starting from 0 but are same as parent array index. You have to use foreach for that. To index the way you want can be done like this:
$info = array(
0 => array (
0 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
2 => array (
2 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
),
3 => array (
3 => array (
'username' => 'wajdi',
'userimage' => 'file_3898.jpg',
'date' => 1373721708,
'postid' => '118',
'type' => 'comment',
'comment' => 'a',
'notify' => '0'
)
)
);
var_dump($info); // original index
$info = array_values($info);
foreach ($info as $key => $value) {
$temp = array();
foreach($value as $k => $v) {
$temp[$key] = $v;
}
$info[$key] = $temp;
}
var_dump($info); // re-index

Categories