Facebook (javascript SDK) create event with image - php

I'm having a hard time creating a facebook event with a picture.
I've tried many things found on stackoverflow, but most of them are using the php SDK.
Facebook documentation also doen't include an example on how to post a photo to the event.
The code I'm using is:
params = {'name': "some_name",
'picture':"some_img_url_at_my_server",
//or 'picture':"#some_img_url_at_my_server",
//or '#picture':"#some_img_url_at_my_server",
//or 'source':"some_img_url_at_my_server",
//or 'source':"#some_img_url_at_my_server",
//or 'picture':"some_img_path_at_my_server",
//or 'picture':"#some_img_path_at_my_server",
//or '#picture':"#some_img_path_at_my_server",
//or 'source':"some_img_path_at_my_server",
//or 'source':"#some_img_path_at_my_server",
'start_time': "some_date"
};
FB.api('/me/events', 'post', params, function (response) {
if (!response || response.error) {
log(response.error);
} else {
alert('Post ID: ' + response.id);
}
});
The event gets created but no picture is shown.
I read somewhere that I have to url encode the picture data to the post I'm making but I don't know the way to do that in order to try it.
I fetching the data from a php page so, I could do that in the php page as well, but again, I don't know what I have to do exactly.
Can anyone please help?
Thank you

It looks like you have to do this in two steps:
Create the event and get its id.
POST to /EVENT_ID/picture with the picture content in the source parameter.
See here for more details.

Related

Tagging friends on Facebook using Graph API

I have developed an app in PHP which
1. process some data on friends of the user (extracted using taggable_friends) and then generates an image and save it on the app server.
2. Shows the image to the user and offers option to share the image on their timeline.
3. Now I need to tag some friends also in that timeline post [as I should use taggable_friends for tagging also not just for getting friends of the user as per Fb guidelines].
I'm using this code to get list of friends
$friends = $fb->get('/me/taggable_friends?fields=name,first_name,id,picture.width(80).height(80)&limit=500')->getGraphEdge()->asArray();
But I don't know how to tag the friends [using PHP] on the timeline while sharing using Javascript. Please help. I can't find an answer to do this job which still works in 2016.
Javascript code to share
function shareOnFacebook() {
var response = '';
FB.ui(
{
method: 'share',
href: linkToShare,
},
function(response) {
if (response && response.post_id) { } else {
alert('Something went wrong !');
}
}
);
};

Get JSON from Vine API with PHP and call it with Jquery Ajax

I'm trying to build a Vine video feed from a certain users timeline. Because Vine does not offer an API, i'm using the unoffical Vine API wich works great!
The following URL for example, returns a JSON with the latest videos from a given Vine channel. https://api.vineapp.com/timelines/users/1127944909723439104?page=1&size=100
Now as far as i know (correct me if i'm wrong) is it not possible to access this JSON directly through an Jquery Ajax/getJSON call.
Because of that, i'm using PHP in combination with a Cronjob to fetch the JSON once a day from the URL and save it in a JSON file on my own server. I'm able to access then this saved JSON file by Jquery Ajax.
That's what i have right now:
PHP:
<?php
file_put_contents("sbxvine.json", fopen("https://api.vineapp.com/timelines/users/1127944909723439104?page=1&size=100", 'r'));
?>
Jquery:
$(function(){
$.getJSON( "sbxvine.json", function( data ) {
var getvines = data.data.records;
for(var i = 0; i < getvines.length; i++){
$('.vinefeed').append('<div class="item"><div class="content"><video width="100%" height="100%" src="' + getvines[i].videoUrl + '" poster="'+ getvines[i].thumbnailUrl+'" loop controls preload="none"></video></div></div>')
}
});
});
Now the problem here is, that i don't know what time the User is uploading new stuff each day, wich means, that the feed is not synced with the Vine channel. And firing a Cronjob every 5Minutes is not what i'm looking for.
So what i need is to get rid of all that Cronjob stuff and fetch the JSON live from the URL when the Ajax call requests it.
To achieve this, i tried doing this:
PHP:
<?php
header('Access-Control-Allow-Origin: *');
$json = file_get_contents('https://api.vineapp.com/timelines/users/1127944909723439104?page=1&size=100');
$obj = json_decode($json,true);
echo $obj;
?>
And then, with Jquery:
$(function(){
$.ajax({
url: 'getvines.php',
dataType: 'json',
type: 'GET',
success: function(data) {
console.log(data);
},
error: function(){
console.log("nope");
}
});
});
Unfortunately, this is not working and i'm not sure what i'm doing wrong. It logs "nope" after one or two seconds.
Or is there a completely different solution to do all this in a better way?
Ahh stupid misstake, i just had to remove json_decode from the php because the URL already returns the JSON in the right format for the Ajax call.
Just an additional Question: Is this a good way to build this feed? What are the cons and what could be recommendations to enhance it (like make it fast as possible or "best practise" options)?

Calling a php function using ajax/javascript

Ok guys I know this question has been asked before but I am very new to PHP and JavaScript and hadn't even heard of ajax until i started looking for an answer to this question so do not understand previous answers.
I am creating a site that essentially is a bunch of videos in a SQL database, it shows one video at a time, I would like to have a next and previous video buttons.
However I cant get past this ajax thing so my question is even simpler. I have looked at this question/answer and think it pretty much sums up what im asking:
How do I run PHP code when a user clicks on a link?
I have copied that exact code,
<script type="text/javascript">
function doSomething() {
$.get("backend.php");
return false;
}
</script>
Click Me!
And in my backend.php file i have literally just got <?php echo "Hello" ?> just to test it and therefore my understanding is that when i click the link the javascript onClick event is trigged which in turn calls the backend.php file, which says to print "Hello" to the page. However when i click the link it does nothing.
Eventually obviously im going to need to get a lot more complex with my php functions and calling variables and all that stuff but i like to figure things out for myself for the most part so i learn. However im stuck on this bit. Also whilst im here i will ask another thing, I want to 'give back' to the users of the site for answering my questions but I can only really well enough in HTML and CSS to answer other peoples questions, any advice on being able to find the simpler questions on here so i can answer some.
Thanks in advance :)
It does nothing becuase you don't do anything with the result. My guess is that in the example you took, it does some work and doesn't show anything to the user. So if you just had some stuff you wanted to run on the server without returning any output to the user, you could simply do that, and it would work.
Example from jQuery's .get() documentation
What you do:
Example: Request the test.php page, but ignore the return results.
$.get("test.php");
What you want to do:
Example: Alert out the results from requesting test.php (HTML or XML, depending on what was returned).
$.get("test.php", function(data){
alert("Data Loaded: " + data);
});
Take a look at the .get() documentation. You're using it incorrectly.
You should be passing data (optional) and handling the data that gets returned, at a minimum:
$.get("backend.php",
{
// data passed to backend.php goes here in
//
// name: value
//
// format. OR you can leave it blank.
}, function(data) {
// data is the return value of backend.php
// process data here
}
);
If you pass data, you can retrieve it on backend.php using $_GET. In this case:
$_GET['name'];
$.get("test.php", { name: "John", time: "2pm" }, function(data) {
alert("Data Loaded: " + data);
});
http://api.jquery.com/jQuery.get/
This would alert the data. right now that function only returns false.
$.get('backend.php', function(data) {
alert(data);
});
Your code will not print to the page the way you have it set up; you're part of the way there, in that you have called the page, but the response needs to be handled somehow. If you open up the developer tools in Chrome, you can click on the Network tab and see the request and response to verify that what you coded is actually working, but now you need to put the response somewhere.
By passing a function as the second variable into $.get, you can make your request show up on the page. Try something like this:
$.get("backend.php", function (data) { $('body').append(data); } );
Your code is not handling with that data. So instead, you should use following code :
$.get("backend.php", function(response) {
alert(response);
})
Or, to show that data on UI, assign it to any html element.
For more understanding , please visit :jQuery.get() link

Facebook group docs api

Is it possible that i can post and retrieve a group's documents with the facebook group api? i have went through the documentation and couldn't find anything that explain how this can be achieved, if anyone know where i can get this please give the link url so that i can go and read it myself. In short i want to retrieve and be able to post documents to a facebook group through a facebook api, either javascript or PHP. basically i'm just looking for a place where i can go and read for myself how this can be done not to be fed with code but if there is any code working out there then don't hesitate to post it.
Thanks
Donald
The only page in the Facebook API docs I found that mentions group documents is this:
https://developers.facebook.com/docs/reference/api/group/
You can retrieve the documents of a group with the docs connection of the group.
Use a URL of this form:
https://graph.facebook.com/group_id/docs?access_token=...
With the ID of a doc, you can request it directly:
https://graph.facebook.com/doc_id?access_token=...
Using the JavaScript SDK:
FB.api('/doc_id', function(doc) {
alert(document.body.innerHTML = doc.message);
});
Posting a doc
(requires permissions publish_stream and manage_groups)
var doc = {
subject: 'Test',
message: 'This is a test doc.'
};
FB.api(group_id + '/docs', 'post', doc, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Doc ID: ' + response.id);
}
});

how to get the url/xmlhttprequest that loads to get the data from a server?

i have this problem that i can't solve for days now...here is my code. i want to get the xmlhttprequest or the url that loads everytime i clicked the $("#btnQuery").
what happened here is when i clicked the button, it will display the data in jqgrid from the server.
$("#btnQuery").click( function() {
var params = {
"ID": $("#eID3").val(),
"dataType": "data"
}
var url = 'process.php?path=' + encodeURI('project/view') + '&json=' + encodeURI(JSON.stringify(params));
$('#tblD').setGridParam({
url:url,
datatype: ajaxDataType,
});
$('#tblD').trigger('reloadGrid');
$('#firstur').append('the url: ' + url+'<br>');//the xmlhttpRequest should disply here in my html
$('#secur').append('response: ' + url+'<br>'); //the response of xmlhttpRequest should display here in my html
});
here's the code of my process.php. this is where i'm going to get the data for my jqgrid.
<?php
print(file_get_contents("http://localhost/" . $_GET["path"] . "?json=" . ($_GET["json"])));
?>
in firebug console, the xmlhttprequest/location that displays is:
http://localhost/process.php?....%22:%22%22,%22Password%22:%22%22%7D
and it's response body is simething like:
{"result":{"ID":"1C1OMk123tJqzbd"}, "time_elapsed":0}
does anybody here knows how to get the url/xmlhttprequest that loads to get the data? and its response body? i wan to display it in my html body aside from my jqgrid...is there anyone who can help me?..please... thank you so much
Well, I'm not suggesting this as good form, but you ought to be able to redefine the send function on the XHR, to intercept any requests going out, grab the original response handler, wrap it in your own handler so you can still call the original function, but insert your own data in there. For a rough example:
Firefox, fix for other browsers:
XMLHttpRequest.prototype.originalSend = XMLHttpRequest.prototype.send;
var myFunction = function(response) {
//do stuff
this.originalReadyStateHandler(response);
}
XMLHttpRequest.prototype.send = function(optional val) {
this.originalReadyStateHandler = this.onreadystatechange;
this.onreadystatechange = myFunction;
this.originalSend(val);
}
Or something to that effect, again, I'm not saying I recommend this, but it could accomplish what it sounds like you're after.
finally, i already figured out how to do solve my own problem.... here's the code:
var myUrl = $("#tblData").getGridParam('url');
myUrl += '&json=' + JSON.stringify(jpar);
//add something here to show the myUrl;
.getGridParam returns the url from options array. you can return some parameters using it. just visit this site: http://www.secondpersonplural.ca/jqgriddocs/_2eb0fi5wo.htm for more info regarding jqgrid methods (methods, parameters, desrciption).
Open the "Network" tab in Firefox' Firebug extension and you'll see what URLs are loaded. Alternatively use Wireshark to see what flows through your cables.

Categories