Getting like and comment counts album of page facebook using graph api - php

Is there a way to check the number likes and comments on my page album? i have make request with this code:
$fields="id,name,description,link,cover_photo,count,likes.limit(0).summary(1),comments.limit(0).summary(1)";
$fb_page_id = "my page id";
$access_token="my access token";
$json_link = "https://graph.facebook.com/v2.7/{$fb_page_id}/albums?fields={$fields}&access_token={$access_token}";
$json = file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
for($x=0; $x<$album_count; $x++){
$id = isset($obj['data'][$x]['id']) ? $obj['data'][$x]['id'] : "";
$name = isset($obj['data'][$x]['name']) ? $obj['data'][$x]['name'] : "";
$url_name=urlencode($name);
$description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";
$link = isset($obj['data'][$x]['link']) ? $obj['data'][$x]['link'] : "";
$cover_photo = isset($obj['data'][$x]['cover_photo']['id']) ? $obj['data'][$x]['cover_photo']['id'] : "";
$count = isset($obj['data'][$x]['count']) ? $obj['data'][$x]['count'] : "";
$likes = count($obj['data'][$x]['likes']) ? $obj['data'][$x]['likes'] : "";
$comments = count($obj['data'][$x]['comments']) ? $obj['data'][$x]['comments'] : "";
But i cant get the data how many likes and comment on my specific album page facebook.
Please does anybody know how to get Facebook album like and comment counts? Many thanks in advance!

Related

POST API Issue - Not functioning

Good afternoon,
I'm having trouble with an api POST request - it simply isn't going anywhere, it's not showing on the API log at all - I've got various get requests which work fine but this one just doesn't - I'm unsure what I am doing wrong.
public function broadband_plan(){
$common = array();
$common['main_menu'] = 'Broadband plan';
$cli_or_postcode = isset($_GET["cli_or_postcode"]) ? $_GET["cli_or_postcode"] : "";
$district_id = isset($_GET["district_id"]) ? $_GET["district_id"] : "";
$sub_premises = isset($_GET["sub_premises"]) ? $_GET["sub_premises"] : "";
$premises_name = isset($_GET["premises_name"]) ? $_GET["premises_name"] : "";
$thoroughfare_number = isset($_GET["thoroughfare_number"]) ? $_GET["thoroughfare_number"] : "";
$thoroughfare_name = isset($_GET["thoroughfare_name"]) ? $_GET["thoroughfare_name"] : "";
$locality = isset($_GET["locality"]) ? $_GET["locality"] : "";
$postcode = isset($_GET["postcode"]) ? $_GET["postcode"] : "";
$nad_key = isset($_GET["nad_key"]) ? $_GET["nad_key"] : "";
$post_town = isset($_GET["post_town"]) ? $_GET["post_town"] : "";
$county = isset($_GET["county"]) ? $_GET["county"] : "";
$district_id = isset($_GET["district_id"]) ? $_GET["district_id"] : "";
$request = array();
$request['apiUsername'] = 'XXXXXXX';
$request['apiPassword'] = 'XXXXXXX';
$request['encryption'] = 'SHA-512';
$request['platform'] = 'LIVE';
$request['method'] = 'POST';
$address = '{
"sub_premises": $sub_premises,
"premises_name": $premises_name,
"thoroughfare_number": $thoroughfare_number,
"thoroughfare_name": $thoroughfare_name,
"post_town": $post_town,
"postcode": $postcode,
"district_id": $district_id,
"nad_key": $nad_key
}';
$address = json_encode($address);
$request['url'] = "/broadband/availability" . $address;
$broadband_plan_detail = array();
if (!empty($address)) {
$broadband_plan_detail = $this->get_plan($request);
}
return view('front.broadband_plan',compact('common','broadband_plan_detail'));
}
The $address after $address = json_encode($address); will be an object, it cannot be concatenated as a string in $request['url'] = "/broadband/availability" . $address;
Please check your API URL and ensure you follow their structure, for example if the API URL accept the parameters as GET request, you can pass the parameters likes:
$request['url'] = "/broadband/availability?sub_premises=" . $sub_premises . "&premises_name=" . $premises_name...
Base on your code, the API method is POST, so you might need to prepare the parameters as an array.

laravel parse large xml file to mysql

$app = new Container;
$document = new OrchestraDocument($app);
$reader = new OrchestraReader($document);
$xml = $reader->load($path);
$xml1 = simplexml_load_file($path);
// print_r($xml1);
$json = json_encode($xml1);
$array = json_decode($json, true);
$clien =$array['cliente'];
//controll empty value
$clien = array_map(function($i) {
$i['indirizzo'] = empty($i['indirizzo']) ? '' : $i['indirizzo'];
$i['cap'] = empty($i['cap']) ? '' : $i['cap'];
$i['citta'] = empty($i['citta']) ? '' : $i['citta'];
$i['prov'] = empty($i['prov']) ? '' : $i['prov'];
$i['piva'] = empty($i['piva']) ? '' : $i['piva'];
$i['cfisc'] = empty($i['cfisc']) ? '' : $i['cfisc'];
$i['luogo_nasc'] = empty($i['luogo_nasc']) ? '' : $i['luogo_nasc'];
$i['data_nasc'] = empty($i['data_nasc']) ? '' : $i['data_nasc'];
$i['sesso'] = empty($i['sesso']) ? '' : $i['sesso'];
$i['tele'] = empty($i['tele']) ? '' : $i['tele'];
$i['mail'] = empty($i['mail']) ? '' : $i['mail'];
$i['cell'] = empty($i['cell']) ? '' : $i['cell'];
$i['cod_card'] = empty($i['cod_card']) ? '' : $i['cod_card'];
$i['cod_card1'] = empty($i['cod_card1']) ? '' : $i['cod_card1'];
$i['punti_card'] = empty($i['punti_card']) ? '' : $i['punti_card'];
return $i;
}, $clien);
$collection = collect($clien);
$collection1 = $collection->chunk(500);
i'm try to import a large xml file into my mysql database.
i'm load my xml file into object, i'm tranform this object into array, i'm control the empty value, and ok.
Now because i have a large query i tranform my array in laravel collection and chunk for isert 500 records at a time, but now i have a new object $collection1. How i enter the query? i have to trasform again in array?
for($i=0;$i<count($collection1);$i++) {
var_dump($collection1[$i]);
$collection1[$i]= stdToArray;
DB::connection()->disableQueryLog();
DB::table('clientis')->insert($collection1[$i]);
foreach( $collection1[$i] as $k=>$v){
var_dump($v);
}
}
thi code don't work
Looking at your code you don't need to create a collection. You can just array_chunk the data:
/*
$collection = collect($clien);
$collection1 = $collection->chunk(500);
*/
$collection = array_chunk($clien, 500, true);

Sync phonebook using php

I am working on web-service to sync contacts using yii2 framework. where i am having whole phone-book (in json array) from mobile end and i have to check which phone numbers are there in my database.
Request params would be something like this
{
"user_id": "4",
"user_details": [{
"first_name": "A",
"last_name": "A"
}, {
"first_name": "B",
"last_name": "B"
,
"mobile_number": ["(888) 888-888", "(777) 777-777"]
}, {
"first_name": "C",
"last_name": "C",
"mobile_number": ["+918000584123", "(666) 666-6666", "(555) 555-5555", "(444) 444-4444"]
}]
}
To achieve the functionality i have done following code.
public function actionSyncPhoneBook()
{
$amResponse = $amResponseData = [];
$snUserId = $this->amData['user_id'];// Here i will get user_id
if (!empty($this->omUser))
{
$userDetails = $this->amData['user_details'];//Here i will get array of user_details
$amAppContacts =[];
$amNonAppContacts = [];
//I have loop here of user details to scan mobile number array
foreach ($userDetails as $userKeys)
{
//In case if mobile number array is not given
if(!empty($userKeys["mobile_number"]))
{
// Loop for scanning mobile numbers
foreach ($userKeys["mobile_number"] as $key => $mobileNumber)
{
//regex for matching mobile number.
$phone = preg_replace('/[^a-zA-Z0-9+]/', '', $mobileNumber);
$oModelUser = Users::find()
->where(['phone' => $phone])
->one();
//Check if conttact is exists or not using exist method
$checkContactExists = Users::find()->where(['phone' => $phone])->exists();
if($checkContactExists == 1)
{
$oModelFriends = Friends::find()->where(["from_user_id"=>$snUserId,"to_user_id"=>$oModelUser->id])->asArray()->one();
$amCheckPhoneNumber['user_id'] = !empty((string)$oModelUser->id) ? (string)$oModelUser->id : '';
$amCheckPhoneNumber['first_name'] = !empty($oModelUser->first_name) ? $oModelUser->first_name : '';
$amCheckPhoneNumber['last_name'] = !empty($oModelUser->last_name) ? $oModelUser->last_name : '';
$amCheckPhoneNumber['email'] = !empty($oModelUser->email) ? $oModelUser->email : '';
$amCheckPhoneNumber['phone'] = !empty($oModelUser->phone) ? $oModelUser->phone : '';
$amCheckPhoneNumber['status'] = !empty((string)$oModelUser->status) ? (string)$oModelUser->status : '';
$amCheckPhoneNumber['relationship_status'] = !empty($oModelUser->relationship_status) ? $oModelUser->relationship_status : '';
$amCheckPhoneNumber['mobile_verified'] = !empty((string)$oModelUser->mobile_verified) ? (string)$oModelUser->mobile_verified : '';
$amCheckPhoneNumber['email_verified'] = !empty($oModelUser->email_verified) ? $oModelUser->email_verified : '';
$amCheckPhoneNumber['latitude'] = !empty($oModelUser->latitude) ? $oModelUser->latitude : "0";
$amCheckPhoneNumber['longitude'] = !empty($oModelUser->longitude) ? $oModelUser->longitude : "0";
if($oModelFriends["is_friend"] == 1)
{
$amCheckPhoneNumber['is_friend'] = "friends";
}
else
{
$amCheckPhoneNumber['is_friend'] = "no_friends";
}
//User Image
if(!empty($oModelUser->user_image))
{
if (filter_var($oModelUser->user_image, FILTER_VALIDATE_URL))
{
if(!empty($oModelUser->user_image))
{
$ssEventDetailsUserImage = $oModelUser->user_image;
}
else
{
$ssEventDetailsUserImage = Yii::$app->params['aws_cover_image_path'];
}
}
else
{
$ssEventDetailsUserImage =Yii::$app->params['aws_cover_image_path'];
}
}
else
{
$ssEventDetailsUserImage = Yii::$app->params['aws_cover_image_path'];
}
//Cover Image
if(!empty($oModelUser->cover_image))
{
if (filter_var($oModelUser->cover_image, FILTER_VALIDATE_URL))
{
if(!empty($oModelUser->cover_image))
{
$ssCoverImage = $oModelUser->cover_image;
}
else
{
$ssCoverImage = Yii::$app->params['aws_cover_image_path'];
}
}
else
{
$ssCoverImage =Yii::$app->params['aws_cover_image_path'];
}
}
else
{
$ssCoverImage = Yii::$app->params['aws_cover_image_path'];
}
$amCheckPhoneNumber['cover_image'] = !empty($ssCoverImage) ? $ssCoverImage : '';
$amCheckPhoneNumber['user_image'] = !empty($ssEventDetailsUserImage) ? $ssEventDetailsUserImage : '';
$amCheckPhoneNumber['app_user'] = "1";
//Final Response of users those who are using application
$amAppContacts[] = $amCheckPhoneNumber;
}
else
{
//Users who does not use application.
$otherUsers['user_id'] = !empty($userKeys['id']) ? $userKeys['id'] : '';
$otherUsers['first_name'] = !empty($userKeys['first_name']) ? $userKeys['first_name'] : '';
$otherUsers['last_name'] = !empty($userKeys['last_name']) ? $userKeys['last_name'] : '';
$otherUsers['email'] = !empty($userKeys['email']) ? $userKeys['email'] : '';
$otherUsers['phone'] = !empty($mobileNumber) ? $mobileNumber : '';
$otherUsers['status'] = !empty($userKeys['status']) ? $userKeys['status'] : '';
$otherUsers['relationship_status'] = !empty($userKeys['relationship_status']) ? $userKeys['relationship_status'] : '';
$otherUsers['user_image'] = Yii::$app->params['aws_cover_image_path'];
$otherUsers['cover_image'] = Yii::$app->params['aws_cover_image_path'];
$otherUsers['mobile_verified'] = !empty($userKeys['mobile_verified']) ? $userKeys['mobile_verified'] : '';
$otherUsers['email_verified'] = !empty($userKeys['email_verified']) ? $userKeys['email_verified'] : '';
$otherUsers['latitude'] = !empty($userKeys["latitude"]) ? $userKeys["latitude"] : "0";
$otherUsers['longitude'] = !empty($userKeys["longitude"]) ? $userKeys["longitude"] : "0";
$otherUsers['is_friend'] = "";
$otherUsers['app_user'] = "0";
//Final Response of users those who are not using app.
$amNonAppContacts[] = $otherUsers;
}
}
}
}
//Merging the app users and non-app users together.
$amResponseData = array_merge($amAppContacts,$amNonAppContacts);
$amResponse = Common:: successResponse("Sync your phonebook.",$amResponseData);
Common::encodeResponseJSON($amResponse);
}
}
From above code i am checking if mobile number is exist in the database than it will push into app users and rests will be in non-app users.
In my database mobile numbers are stored in the format of +(countryCode)(mobileNumber)
Example +918000584123
But it should also work if i don't pass +91 or any country codes.
It should work if mobile number array would be like
"mobile_number": ["(888) 888-888", "(777) 777-777"]
or it should be like
"mobile_number": ["+918000584123","+91 8000584123","8000584123", "(666) 666-6666", "(555) 555-5555", "(444) 444-4444"]
How to achieve this functionality?
Any help would be appreciated.
Thanks in advance.
dont need regex like this
$phone = preg_replace('/[^a-zA-Z0-9+]/', '', $mobileNumber);
Instead you can use
$phone = preg_replace('/[^0-9]/', '', $mobileNumber);
and change your where condition like this below. You should use LIKE condition
$oModelUser = Users::find()->where("phone LIKE'%".$phone."'")->one();
$checkContactExists = Users::find()->where("phone LIKE '%".$phone."'")->exists();
You tagged it with mysql, so I am answering from that angle.
The various phone number should be in a separate table, together with an id of the person they belong to. That way, there can be an arbitrary number of numbers for any person. I suggest 1:many, not many:many, and simply let the occasional shared phone be a dup number in the new table.
The table would have a non-unique index on number, thereby making it 'trivial' in SQL to locate the person(s) with a given number (or set of numbers, using IN).

How can I reply a picture and count users on telegram bot?

I wanted to made a telegram bot so I started coding and this is what I have so far:
<?php
$string = json_decode(file_get_contents('php://input'));
function objectToArray( $object )
{
if($text == 'Hi')
$text_reply = 'Hi';
if($text == 'What is you'r name?)
$text_reply = 'testbot';
if($text == 'How are you ?')
$text_reply = 'Fine,Thanks';
}
$token = '';
$url = 'https://api.telegram.org/bot'.$token.'/sendMessage?chat_id='.$user_id;
$url .= '&text=' .$text_reply;
$res = file_get_contents($url);
?>
I didn't try it before but it's very simple and I think it will work.
How can I reply a photo from a URL ( someone ask it before but i want it in php) ?
How can I count users or save their ID in a database ? ( ex : when a user send /statics command if id=x reply members number ( when anyone send /start +1 to members number . else reply x )

Search based on URL varibles

I want my search to focus on variables in the URL string. Currently it uses a form based search.
It uses this
<?php
include('db.php'); // include your code to connect to DB.
$tbl_name="mobile"; //your table name
$whereClauses = array();
if (! empty($_POST['Model'])) $whereClauses[] ="model='".mysql_real_escape_string($_POST['Model'])."'";
if (! empty($_POST['Mins'])) $whereClauses[] ="minutes='".mysql_real_escape_string($_POST['Mins'])."'";
if (! empty($_POST['Texts'])) $whereClauses[] ="texts='".mysql_real_escape_string($_POST['Texts'])."'";
if (! empty($_POST['Freegifts'])) $whereClauses[] ="free_gift='".mysql_real_escape_string($_POST['Freegifts'])."'";
if (! empty($_POST['Network'])) $whereClauses[] ="network_name='".mysql_real_escape_string($_POST['Network'])."'";
if (! empty($_POST['Merchant'])) $whereClauses[] ="merchant_category='".mysql_real_escape_string($_POST['Merchant'])."'";
$where = '';
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); }
$sql = mysql_query("SELECT * FROM $tbl_name".$where);
?>
However, I have added this to my page:
<?php
$model = $_GET['model']; //gets model from URL
$mins = $_GET['mins']; //gets mins from URL
$texts = $_GET['texts']; //gets mins from URL
$freegift = $_GET['free-gift']; //gets mins from URL
$network = $_GET['network']; //gets mins from URL
$plan = $_GET['plan']; //gets mins from URL
?>
It needs to be so not all variables are required. Any help would be appreciated.
Thanks in advance :)
There are a number of options that will return value or NULL:
<?php
$model = (isset($_GET['model']) ? $_GET['model'] : NULL);
$mins = (isset($_GET['mins']) ? $_GET['mins'] : NULL);
$texts = (isset($_GET['texts']) ? $_GET['texts'] : NULL);
$freegift = (isset($_GET['free-gift']) ? $_GET['free-gift'] : NULL);
$network = (isset($_GET['network']) ? $_GET['network'] : NULL);
$plan = (isset($_GET['plan']) ? $_GET['plan'] : NULL);
?>
This will get the variables from their correponding $_GET element, only if there is one set.
Alternative syntax (shorter):
<?php
$model = ($_GET['model'] ? $_GET['model'] : NULL);
$mins = ($_GET['mins'] ? $_GET['mins'] : NULL);
$texts = ($_GET['texts'] ? $_GET['texts'] : NULL);
$freegift = ($_GET['free-gift'] ? $_GET['free-gift'] : NULL);
$network = ($_GET['network'] ? $_GET['network'] : NULL);
$plan = ($_GET['plan'] ? $_GET['plan'] : NULL);
?>
Alternative (untested though):
<?php
$vars = array('model','mins','texts','free-gift','network','plan');
foreach($vars as $value) {
$$value = (isset($_GET[$value]) ? $_GET[$value] : NULL);
}
?>
EDIT:
In order to set a WHERE clause based on it, I suggest:
<?php
$vars = array('model','mins','texts','free-gift','network','plan');
foreach($vars as $value) {
$$value = (isset($_GET[$value]) ? $_GET[$value] : NULL);
unset $vars[$value]; //sweeping the NULL ones
}
$where_clause = $vars[0]; //the only remaining value after previous cleanup
?>

Categories