Extracting data with preg_match - facebook

I'm trying to extract Facebook members name with preg_match from gaph.facebook.com, by userID. The script doesn't seem to be working. can anyone help?
<?php
$content = file_get_contents('https://graph.facebook.com/myid?fields=id,name,first_name,last_name,picture');
preg_match("'\"name\": \"(.*?)\",'si", $content, $match);
$name = $match[1];
?>

I don't understand why would you use PHP preg_match() with REGEX instead of using json_decode() since you're already dealing with a JSON type response
<?php
$content = file_get_contents('https://graph.facebook.com/{ID}?fields=id,name,first_name,last_name,picture');
$jsonData = json_decode($content);
echo '</br> id: ' .$jsonData->id,
'</br> name: ' .$jsonData->name,
'</br> first_name: ' .$jsonData->first_name,
'</br> last_name: ' .$jsonData->last_name,
'</br> picutre url: '.$jsonData->picture->data->url;
?>
Example

Related

PHPmailer: Exclude input field from foreach loop

I am happily running a contact form using https://github.com/PHPMailer/PHPMailer and recently added https://friendlycaptcha.com to minimize the amount of spam sent by bots through that form. I love the sleek implementation with no user interaction. The captcha is activated once you click the first input field in the form and auto-solves.
Unfortunately the captcha's solution (hidden input field) adds an enormous string to the e-mail that I would like to exclude.
How do I tell the foreach loop not to include the frc-captcha-solution in the mail that's composed?
private function compose_mail($post)
{
$content = "The following message has been received via contact form:\n\n";
foreach($post as $name=>$value)
{
$content .= ucwords($name).": \t";
$content .= "$value\n\n";
}
$this->mailer->Body = $content;
}
This is the input field in the form that is generated and yields the long string.
<input name="frc-captcha-solution" class="frc-captcha-solution" type="hidden" value=".UNSTARTED">
An example of what I get is:
name: Joe
e-mail: joe#miller.com
address: 100 street
ZIP: 10100
place: City
phone: 0123456789
msg: This is a test message
Frc-captcha-solution: f5e6874becd6758f456b78f6f1726e1d.YadsJVSp+/j0pyBNAQwtjQAAAAAAAAAA+eFoDacaj3c=.AAAAAJZoAQABAAAAQ5YAAAIAAADCJAUAAwAAAF9IAQAEAAAAj5cEAAUAAACwkQMABgAAAFcaAAAHAAAAfvIEAAgAAADvUwIACQAAAK/MAQAKAAAAm+MBAAsAAACU6AAADAAAAJZUAAANAAAA/dAEAA4AAABrSwEADwAAAC7mAAAQAAAAXFoDABEAAAD4UAEAEgAAACMEAAATAAAA3xcCABQAAABBHwMAFQAAAE6LAQAWAAAA1mwAABcAAAAmwwAAGAAAACIZBwAZAAAA6p4AABoAAADovwIAGwAAAFE/AAAcAAAA+d8BAB0AAACQ8gAAHgAAAO1fAgAfAAAAbtwCACAAAAA7EgEAIQAAABmWAQAiAAAA/ysAACMAAAAx0wIAJAAAAJsyBAAlAAAA6acBACYAAACATwAAJwAAAM/wBQAoAAAATLYJACkAAABT1AIAKgAAAMWWAwArAAAAzIAGACwAAAA3fwAA.AgAA
There are many ways to do that, but one is to skip that name when it appears in your loop:
private function compose_mail($post)
{
$content = "The following message has been received via contact form:\n\n";
foreach($post as $name => $value) {
if ($name !== 'frc-captcha-solution') {
$content .= ucwords($name).": \t";
$content .= "$value\n\n";
}
}
$this->mailer->Body = $content;
}

List facebook likes underneath every url

I was trying to find the best way to count number of likes for a facebook page url and after googling a lot and playing with the code, i have a code like the one given below. It outputs the likes, name of the page and then the link. I wish to know:
1. How can i use the html tag to convert the link into hyperlink so that I can have something like "Click here to visit"
2. How can monitor performance of 25+ fb_id on an hourly basis with a sorted order (descending) on likes
<?php
$fb_id = '36922302396';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
printf("%s %s %s", $result->likes, $result->name, $result->link);
?>
Edited code as per solution provided
<?php
$fb_id = '36922302396';
$pic = 'https://www.facebook.com/' . urlencode($fb_id) . '/picture?type=square';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
echo $result->likes , " " , $result->name , " " , "<a target='_blank' href=\"" . $result->link . "\" ><img src = $pic></a>";
?>
Thanks
For 1)
How about
<?php
$fb_id = '36922302396';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
//printf("%s %s %s", $result->likes, $result->name, $result->link);
echo "Link";
?>
You can also use
Link
For 2)
If you know the Page's IDs, then you can just concatenate them to the following :
GET /?ids=40796308305,339150749455906&fields=id,name,likes,talking_about_count
The result looks like the following
{
"40796308305": {
"id": "40796308305",
"name": "Coca-Cola",
"likes": 88365218,
"talking_about_count": 635936
},
"339150749455906": {
"id": "339150749455906",
"name": "Pepsi",
"likes": 33321925,
"talking_about_count": 208761
}
}
For hourly stats, you need to setup your script via cron etc. and write the results to a database.

Auto complete/Suggest in Wordpress Form

I'm placing a search form of 6 fields on my home page which includes a text box field named course. I want to show course suggestions while user typing. One more is, I want to show/hide some fields according to the option of first field dropdown. Any help would be appreciated.
You can use jQuery Auto Suggest which is included with WordPress : wp_enqueue_script
With this you can write a form that does a Ajax lookup to the the Ajax URL handler. Which you can add_action onto. AJAX in Plugins
So you can ajax lookup and then on the action side you can just perform a get_posts to match titles, or a raw sql Query. And return what is needed. edit your functions.php.
add_action('wp_enqueue_scripts', 'se_wp_enqueue_scripts');
function se_wp_enqueue_scripts() {
wp_enqueue_script('suggest');
}
add_action('wp_head', 'se_wp_head');
function se_wp_head() {
?>
<script type="text/javascript">
var se_ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery(document).ready(function() {
jQuery('#se_search_element_id').suggest(se_ajax_url + '?action=se_lookup');
});
</script>
<?php
}
add_action('wp_ajax_se_lookup', 'se_lookup');
add_action('wp_ajax_nopriv_se_lookup', 'se_lookup');
function se_lookup() {
global $wpdb;
$search = like_escape($_REQUEST['q']);
$query = 'SELECT ID,post_title FROM ' . $wpdb->posts . '
WHERE post_title LIKE \'' . $search . '%\'
AND post_type = \'post_type_name\'
AND post_status = \'publish\'
ORDER BY post_title ASC';
foreach ($wpdb->get_results($query) as $row) {
$post_title = $row->post_title;
$id = $row->ID;
$meta = get_post_meta($id, 'YOUR_METANAME', TRUE);
echo $post_title . ' (' . $meta . ')' . "\n";
}
die();
}
Use ajax for both. You may have to write some mysql query to retrieve the required fields(post titles or whatever it is) from the table.

replace text with url in twitter feed

I have this code:
public function linkify($status_text)
{
$status_text = preg_replace('/(https?:\/\/\S+)/','\1', $status_text);
$status_text = preg_replace('/(^|\s)#(\w+)/','\1#\2',$status_text);
$status_text = preg_replace('/(^|\s)#(\w+)/','\1#\2',$status_text);
return $status_text;
}
and display feeds from twitter like this
foreach($feed as $feed_item) {
$html .= '<li>';
$html .= '' . $this->linkify($feed_item->text) . '';
$html .= '' . $this->relativedate((strtotime($feed_item->created_at))) . '';
$html .= '</li>';
}
echo $html;
result of this code is
<li>Twitter Feed Text http://t.co/TnkNfxCdRu</li>
if anyone can help me, how can I add text inside tag <a></a>. for example, to be exactly like this:
<li>Twitter Feed Text</li>
Thank you so much
Just change your first preg_replace to:
$status_text = preg_replace('~(https?://(\S+))~','$2',$status_text);

Need help formatting Unix Date from XML output.

I have an events feed from facebook that outputs data in XML format. The dates/ times are in epoch (unix) format, I think. Like so:
<start_time>1319506200</start_time><end_time>1319511600</end_time>
This is dynamic information (events created by a facebook page).
I am using php file_get_contents to place the xml output in my html.
How in the world can I convert the unix dates to a user friendly format? I am at a total loss.
Simply use string date ( string $format [, int $timestamp = time() ] )
Extract the timestamp value from the xml string and pass in as argument #2 to date, as argument #1 you should supply your preferred format string, for example 'Y-m-d T:i:s'
Extracting he timestamp could probably look something like this:
Given that you have the event feed output data stored in a string, in this case $xmlstr
$facebooksomething = new SimpleXMLElement($xmlstr);
date('Y-m-d T:i:s', $facebooksomething->starttime);
Finally got this figured out! :) Woohoo! (changed from the xml feed to json feed by the way) This is what ended up working:
$start_date = date('F j, Y, g:i a', strtotime($json_output2->start_time));
$end_date = date('g:i a', strtotime($json_output2->end_time));
Here is my full code:
<?php
$jsonurl = "https://graph.facebook.com/PAGEID/events?access_token=MYYOKEN";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->data as $data)
{
$jsonurl2 = "https://graph.facebook.com/$data->id/";
$json2 = file_get_contents($jsonurl2,0,null,null);
$json_output2 = json_decode($json2);
$start_date = date('F j, Y, g:i a', strtotime($json_output2->start_time));
$end_date = date('g:i a', strtotime($json_output2->end_time));
echo "{$json_output2->name}\n";
echo "<br>";
echo "{$start_date}\n";
echo " - ";
echo "{$end_date}\n";
echo "<br>";
echo "{$json_output2->description}\n";
echo "<br>Where: ";
echo "{$json_output2->location}\n";
echo "<br><br>";
}
?>