PHP-Mongodb: How to check the nested array exist or not - mongodb

I am working on backend with mongodb & php. I have stored message content in mongodb database.
My mongodb database content is this
.......some content.........
"content": "This is the test message",
"time_created": 1418813065,
"status": "read",
"agencyStatus": "read",
"reply": [
{
"email": "leon#accenture.com",
"name": "Accenture",
"content": "Thanks for your test message",
"time": 1418813145
}
]
In my program i am retriving the message content & reply content through foreach loop as is nested as a object inside.
<?php echo $mongoMessage['content'];?>
<?php foreach ($mongoMessage['reply'] as $mongoReply){ ?>
<?php echo $mongoReply['content']; ?>
My problem is when a record with reply exist i am retriving then it is displaying on browser but a record with no reply, i am retriving then it shows me notice on browser. How to get rid of this situation. This problm i am facing from last week plz help me guys...

If I could get you right then you are getting a notice in browser when you try to retrive a document where reply array does not have a value.
So to get rid of that you must check first if the reply array is not empty. Like this -
<?php
echo $mongoMessage['content'];
if (!empty($mongoMessage['reply'])) {
foreach($mongoMessage['reply'] as $mongoReply)
{
echo $mongoReply['content'];
}
}
?>
Hope this helps :-)

Related

How do I reference and create dynamic emails with Sendgrid?

I'm at a loss with the documentation here.
I'm trying to create a basic hello world email template, with some simple email substitutions - and I can't work out the syntax.
The documentation here suggests using a -igetreplaced- syntax, and gives the following example:
<html>
<head></head>
<body>
<p>Hello -name-,<br>
Thank you for your interest in our products. I have set up an appointment to call you at -time- EST to discuss your needs in more detail. If you would like to reschedule this call, please visit the following link: `reschedule`
Regards,
-salesContact-
-contactPhoneNumber-<br>
</p>
</body>
</html>
An accompanying SMTP API JSON header might look something like this:
{
"to": [
"example#example.com",
"example#example.com"
],
"sub": {
"-name-": [
"John",
"Jane"
],
"-customerID-": [
"1234",
"5678"
],
"-salesContact-": [
"Jared",
"Ben"
],
"-contactPhoneNumber-": [
"555.555.5555",
"777.777.7777"
],
"-time-": [
"3:00pm",
"5:15pm"
]
}
}
I've tried pasting these in to the template code editor, but it doesn't work.
Can someone point me to the right documentation for getting the syntax correct?
The syntax is Handlebars.
Use it like this:
Hello {{name}}
with data:
{
"name": "Bob"
}

How to perform PATCH operation in Firebase APi?

The firebase doc sys this is how it is supposed to be done:
curl -X PATCH -d '{"last":"Jones"}' \
'https://[PROJECT_ID].firebaseio.com/users/jack/name/.json'
But I dont know how to convert this to a rest based request.
TO be clear I need to send a web request from javascript/java, hence I want to know what should be the body , and header and operation type for this request.
Can someone please help?
If you use the documentation for curl, you can figure out what that command line you showed is trying to tell you.
The HTTP method is: PATCH
The request body is: {"last":"Jones"}
The url is: https://[PROJECT_ID].firebaseio.com/users/jack/name/.json
Where PROJECT_ID is the name of your project. That's all there is to it.
You need teh following structure:
HTTP Request:
https://firestore.googleapis.com/v1/projects/*YOUPROJECT_ID*/databases/(default)/documents/users_admin/*DOCUMENT_ID*?**updateMask.fieldPaths=user_name&updateMask.fieldPaths=permisos.Administrador&updateMask.fieldPaths=user_email**
JSON Body (must be exactly the same structure and type as your database):
{
"fields": {
"user_name": { "stringValue": "Test ActualizaciĆ³n 2" },
"permisos": {
"mapValue": {
"fields": {
"Administrador": {
"booleanValue": true
}
}
}
},
"user_email": { "stringValue": "veviboj548#eyeremind.com" }
}
}

Including and using Zend Service ReCaptcha in ZF2 (v2.3.3)

how to include Recaptcha service in zend framework 2?
I tried to do like this:
public function contactAction()
{
$formContact = new ContactForm();
$pubKey = 'mypubkey';
$privKey = 'myprivkey';
$recaptcha = new ZendService\ReCaptcha\ReCaptcha($pubKey, $privKey);
return array ('formContact' => $formContact, 'recaptcha' => $recaptcha);
}
but I discovered that ZendService\ReCaptcha is not present by default when you download the framework.
So, I downloaded it from here
https://github.com/zendframework/ZendService_ReCaptcha
and I placed it into vendor\zendframework\zendframework\library\zend together with the other parts of the library.
I tried to refresh the page but doesn't work again because it can't find the zend service recaptcha.
Fatal error: Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found in C:\Program Files (x86)\xampp\htdocs\Zf-tutorial\module\Application\src\Application\Controller\IndexController.php on line 79
can someone help me? I thought it was simple to implement recaptcha, but it is not so ! thanks!
Add the zendservice-recaptcha module to your composer.json file and run an update:
{
...
"repositories": [
{
"type": "composer",
"url": "http://packages.zendframework.com/"
}
],
...
"require": {
...
"zendframework/zendservice-recaptcha": "*",
...
}
...
}
update composer :
php composer.phar update
This will install the module and configure the relevant class mapping and you will be able to access the classes by adding the use statements as with any other classes you use.
Even i tried recaptcha but with no success so implemented something different to refresh captcha and worked very well, try this once
resetCaptcha function:
$form = $this->getServiceLocator()->get('zfcuser_register_form');
$captcha = $form->get('captcha')->getCaptcha();
$data = array();
$data['id'] = $captcha->generate();
$data['src'] = $captcha->getImgUrl() .
$captcha->getId() .
$captcha->getSuffix();
return $data;
ajax request :
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
var data = [];
var form = <?php $this->registerForm; ?>
data.push({name: "action", value: 'resetCaptcha'});
data.push({name: "params[form]", value: form});
$.post("<?php echo BASE_URL ?>/user/iajax", data,
function(data) {
$('#form_reg img').attr('src', data.src);
$('#captcha-id-hidden').attr('value', data.id);
}, 'json');
});
});
Html call :
<p class="refresh_captcha"><?php echo $this->formCaptcha($form->get('captcha')); ?>
<input type="button" id="refreshcaptcha" value="refresh">
</p>
You do not properly install the library ZendService\ReCaptcha.
your system write:
Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found
You must:
placed it into vendor\zendframework\zendframework\library
In the file vendor/ZF2/library/Zend/Loader/StandardAutoloader.php insert string
$this->registerNamespace('ZendService', dirname(dirname((__DIR__)))
. '/ZendService');
in case self::AUTOREGISTER_ZF:
in the file init_autoloader.php insert string
$loader->add('ZendService', $zf2Path);.

Not getting post notification from facebook for realtime api

I'm trying to get the realtime updates to my app from facebook using their realtime api. I have configured my subscription.
{
"data": [
{
"object": "user",
"callback_url": "https://myappname.herokuapp.com/realtime.php",
"fields": [
"activities",
"feed",
"is_app_user",
"likes",
"online_presence",
"photos",
"picture",
"status",
"wall_count"
],
"active": true
}
]
}
I have created an endpoint which simply logs the request when I get a post request. But I don't seem to be getting any calls from facebook. If I manually do a post to the end point, it logs the post body json. I have checked the heroku logs but there is no trace of post request form fb, only my manual request is present.
<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe') {
echo $_GET['hub_challenge'];
} else if ($method == 'POST') {
$data = file_get_contents("php://input");
$json = json_decode($data, true);
error_log("update recieved");
error_log('updates = ' . print_r(json, true));
pg_close($db);
}
?>
I have did a callback request test specified by
https://graph.facebook.com/yourappid/subscriptions?access_token=youraccesstoken&object=user&fields=feed&verify_token=yourownsecretstringsetinyourphpfile&method=post&callback_url=http://www.yourwebsite.com/facebook_subscribe.php
which returned me null indicating subscription is success.
I have made a few status updates and likes but I'm not getting any updates from fb. Am I missing something out here?
Thanks in advance.
It's a bug but FB has labelled it as invalid.
Upvote this: https://developers.facebook.com/bugs/394779173962716

How To Check If a FACEBOOK ID is male or female

I was wondering can we check the gender of the facebook users using the facebook ID..
Yes you can, use the Graph API to query a user, here is my object, as you can see, I have a property called gender, I believe this property can be hidden if the user has set it up as such, so never assume this is in the response...
This also requires the target user to have granted your application basic information access.
{
"id": "754124803",
"name": "Daniel Phillips",
"first_name": "Daniel",
"last_name": "Phillips",
"link": "https://www.facebook.com/daniel.j.p",
"username": "daniel.j.p",
"gender": "male",
"timezone": 0,
"locale": "en_GB",
"verified": true,
"updated_time": "2012-02-04T16:00:42+0000",
"type": "user"
}
If you simply want to test out this, then use the Graph API Explorer: https://developers.facebook.com/tools/explorer/
You didn't mention which SDK you use i assume you use PHP
below is the php code and i think if you can use any other SDK you can also able to use the
below code with some necessary changes and yes this code is work perfectly i tested this code before answer here :)
<?php
//get the user gender
$user_id = $facebook->getUser();
$user_profile = $facebook->api('/me','GET');
$user_gender = $user_profile['gender'];
echo "Gender: " . $user_gender;
?>
You need user_about_me permission if user gender is not public and you can also get user friends gender using friends_about_me permission if user friend set this public
below is the my authorization code you need to make some changes in your authorization code use the above code in scope
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=email,read_stream,publish_stream,offline_access,publish_actions,user_about_me,fiends_about_me&response_type=token"