FB batch request : get Profil Images of managed Pages - php

Hi there i tried to get all the Profile-Images of the Pages i managed.
I tried something like this:
// my loop:
$request[] = array(
'method' => 'POST',
'relative_url' => '/'.$account->id.'/?fields=picture.type(large)&access_token='.$account->access_token.''
);
and then:
$batchResponse = $this->facebook->api('?batch='.json_encode($request),'POST',array('access_token' => $this->facebook->getAccessToken()));
without success :/
i also tried to set the access_token of the page in the body tag:
$request[] = array(
'method' => 'POST',
'relative_url' => '/'.$account->id.'/?fields=picture.type(large)'
'body' => '&access_token='.$account->access_token.''
;

Your PHP script should have you login. If the login was successful, the SDK will manage the access_token for you.
Then issue this one request:
$result = $this->facebook->api('/me/accounts?fields=name,picture.type(large)', 'GET');
If you don't want to authenticate, instead you can pass an array of ids:
$page_ids = array( _PAGE_ID_1, _PAGE_ID_2, ...);
$result = $this->facebook->api('/?ids=' . implode(',',$page_ids) . '&fields=name,picture.type(large)', 'GET');
For these queries, you are getting data, so you should be using a GET request. Within the Facebook API, POST requests are only used when you are sending data to Facebook.

Related

HTTPS REQUEST WITH stream_context_create()

I have problem with my code, the error I get is:
Warning: fopen(https://discordapp.com/api/v6/auth/login): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in
C:\xampp\htdocs\s2.php on line 15
Here is the relevant Code:
<?php
$data = array("email" => 'Email#gmail.com', "password" => 'Password');
$data_string = json_encode($data);
$context_options = array (
'https' => array (
'method' => 'POST',
'header'=> "Content-type: application/json\r\n"
. "Content-Length: " . strlen($data_string). "\r\n",
'content' => $data_string
)
);
$context = stream_context_create($context_options);
$fp = fopen('https://discordapp.com/api/v6/auth/login', 'r', false, $context);
?>
Thank you for your help!
It seems directly logging in via a bot is no longer allowed as you should use the OAuth2 (how does OAuth2 work?) functionality of Discord. This means that your bot needs to be set up inside your Discord account and then you may use token-based access on the bot to authenticate the external application against Discord.
The change to no longer allow bot-based logins happened around the beginning of 2017 and at that point all PHP-based Discord-related Github applications stopped to be maintained. Here is a discussion and comment about banning bots with automated login and this one about that OAuth2 has to be used.
Read more about authentication and bots in the Discord OAuth2 chapter.
If you can tell more about what you plan to achieve, maybe we can help you find a solution to your task.
Previous (no-longer working) answer:
I haven't used DiscordApp so I haven't tried this yet. Instead of sending JSON-encoded data, have you tried posting the values as FORM values to the API?
$postData = array(
'email' => 'Email#gmail.com',
'password' => 'Password'
);
$params = array('https' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postData
)
);
$context = stream_context_create($params);
// get the entire response ...
$result = file_get_contents('https://discordapp.com/api/v6/auth/login', false, $context);
// ... or alternatively using fopen()
$fp = fopen('https://discordapp.com/api/v6/auth/login', 'r', false, $context);
I haven't found any docs regarding how the parameters should be passed to the login URI but at least that's how normal form-based logins can be used.
According to the DiscordApp Response codes a 400 may be received if either the call was not understood (non-existent function called) or the parameters were send improperly so you should find out about how to call the logininterface with parameters using scripts.

Validation data using api

I need to validate some form data both in client-side and server-side (php). I can use some API web service to check the data form. The problem is: I can validate the data, in client side, using an Ajax call and my API web service. How can I use the API web service from server-side?
You can use file_get_contents to invoke external call
$homepage = file_get_contents('http://www.example.com/');
And there you can send the parameter from the url itself,
Here the example to send parameter to your api
Construct an array and place your values to be validated
$getdata = http_build_query(
array(
'name' => 'Francesco',
'phone' => '2434343',
'age'=>'23',
'city'=>'MA',
'state'=>'US'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $getdata
)
);
$context = stream_context_create($opts);
Then send it to your api's directly
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);

When calling the MediaWiki-API I only get '+\' as an csrf-token

I can edit with the simple token '+\' but with this simple token I can edit only as an unregistered IP, not as an registered user. Can someone help me?
My PHP code:
$parameters = array('action' => 'query', 'meta' => 'tokens', 'format' => 'json');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($parameters),
),
);
$context = stream_context_create($options);
$result = file_get_contents($wiki, false, $context);
echo "$result";
You need to be logged in to get a real CSRF token. For details on how to log in, see:
http://www.mediawiki.org/wiki/API:Login
Note that staying logged in will require that you store cookies across requests. This will be difficult with file_get_contents(); using cURL with a cookie jar is recommended.
To get a login token use the type=login parameter. Example:
curl 'https://www.mediawiki.org/w/api.php?action=query&meta=tokens&format=json&type=login'

Facebook graph api /me/photos issue

I am currently posting images URLs on different facebook pages but I am using many calls to do the same thing.
Instead of this, I am trying to work on a batch request to save on execution time but I have an issue.
Doing many calls, I use this:
$urlLink = '/' . $pageId . '/photos';
$args = array(
'url' => $this->image_url,
'message' => $this->message,
'published' => false,
'scheduled_publish_time' => strtotime($this->programmed_dt),
);
$res = $this->fb->api($urlLink, 'POST', $args);
It works fine.
With the batch request I tried with that:
$urlLink = '/' . $facebookPage['id'] . '/photos';
$args['access_token'] = $facebookPage['access_token'];
$queries[] = array('method' => 'POST',
'relative_url' => $urlLink,
'body' => $args,
'url' => $this->image_url
);
$res = $this->fb->api('?batch=' . json_encode($queries), 'POST');
The response I have is:
{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}
I tried to change the name field with all possibilities to send the image link, without success...
Any ideas for batch requests with image urls?
According to https://developers.facebook.com/docs/graph-api/making-multiple-requests/#multiple_methods I assume that your code which forms the request doesn't have the correct syntax.
From my point of view it should be the following:
$args['access_token'] = $facebookPage['access_token'];
$queries[] = array('method' => 'POST',
'relative_url' => $urlLink,
'body' => 'url=' . $this->image_url
);
$res = $this->fb->api('/?batch=' . json_encode($queries) . '&access_token=' . $args['access_token'], 'GET');
I can see everything ok in your code except this-
You should set upload support to true before posting image.
$this->fb->setFileUploadSupport(true);
for more detail you can check this answer:
CHECK HERE

Retrieve authentication token from api via post

Im trying to build an app that uses the simplenote api but I am having trouble with the authentication part. I am getting a 400 error(bad request).
Im guessing this issue is not related to the simplenote api, it's rather my understanding of the documentation.
Here is what the api is saying:
HTTP POST to the following URL:
https://simple-note.appspot.com/api/login
The body of the request should contain this, but base64-encoded:
email=[email-address]&password=[password]
To be clear, you will concatenate email= with the user’s email address (trimmed),
&password=, and the user’s password; then base64 the resulting string and send it as
the HTTP request body. (Do not encode this request using the standard variable
encoding used for POSTed web forms. We basically ignore the Content+Type header
for this request, but text/plain makes the most sense here.)
And here is my code so far:
$url = 'https://simple-note.appspot.com/api/login';
$data = array('email' => base64_encode($email), 'password' => base64_encode($password));
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
I think what they mean is:
$data = base64_encode(http_build_query(array(
"email" => $email,
"password" => $password
));
So base64 encode the resulting string, not the individual parts.
As for making the request. I can recommend you take a look at cURL, it's much faster than file_get_contents.

Categories