So i need to gain access to a web service containing some json, but to do so I was told to make use of PHP POST method to first log into the web service. I was giving an array with 3 types/values.
{
"Username":"user",
"password":"1234",
"LoginClient":"user"
}
I have been searching all day for a solution, but have come up short :(.
Any advice or push into a right direction would be much appreciated.
Hope I have explained this clearly enough.
you could do as follows:
$url = 'http://yourDomain.net/api/auth/';
$data = array('Username' => 'user', 'password' => '1234', 'LoginClient' => 'user');
$opts = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($opts); //Creates and returns a stream context with any options supplied in options preset.
$response = file_get_contents($url, false, $context);
var_dump($response);
Or you could read about CURL as another option to make POST requests.
Related
I'm trying a new service to do a specific task, they have a pretty basic API which lets the users build simple apps.
An example of the HTTP request is:
https://domainname.com/dashboard/api
?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}
&email={YOUR EMAIL}&api_secret={API SECRET}
&unicode={TRUE/FALSE}&id={IDENTIFIER}
I have tried everything, using postman,php and googling for the past 3 hours and i can't get it to work.
(even tried to send it through the browser lol)
Whats a proper way to send a http request?
Thank you.
You can send an HTTP request without CURL using PHP5. This answer is based off of How do I send a POST request with PHP?. You'll have to enter your variables into the $data array.
$url = 'https://domainname.com/dashboard/api';
$data = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$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);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
You can test if your requests are working from your script by using http://www.postb.in or some other similar service. This allows you to see your requests to debug them (rather than relying on feedback/errors from the service you are using). Then you'll know if your requests are formatted correctly and working. Sometimes a service is down... and you spend hours troubleshooting something that's not on your end.
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.
I am making a FB messenger bot. I can not get the part with making the bot sending a message back to work.
I am using Heroku for the Webhook.
I am following this tutorial: https://www.youtube.com/watch?v=__SWFhHocDI
Here is my code (I changed the token variable, I do use the real token):
file_put_contents('fb.txt', file_get_contents('php://input'));
$fb = file_get_contents('fb.txt');
$fb = json_decode($fb);
$rid = $fb->entry[0]->messaging[0]->sender->id;
$token = "MYTOKEN";
$data = array(
'recipient' => array('id'=>"$rid"),
'message' => array('text'=>"Nice to meet you!")
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\n"
)
);
$context = stream_context_create($options);
file_get_contents("https://graph.facebook.com/v2.8/me/subscribed_apps?access_token=$token", false, $context);
Can you find any problems? :-) Thank you for taking time.
maybe you should try something like this -
file_get_contents("https://graph.facebook.com/v2.6/me/messages?access_token=$token", false, $context);
Well, I know that the headline look simple, but i was looking from 3 days for an example on how to make the POST request to webapi.
Currently I am using JQuery to do my POST, but I need some php script to run and talk to my C# webAPI, and it seems impossible to find some examples or explain on how to do that.
Someone gave me then Code :
$response = file_get_contents('http://localhost:59040/api/Email/SendEmails');
$response = json_decode($response);
echo ($response);
But this one does nothing - Not even an error on how to go more into the problem.
I simpley need a php script to make the POST request to webapi who gets 1 param(String) and return An ok answer or Error,
After Maalls answer from this post How do I send a POST request with PHP?
The answer was really simple and the code was the following :
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$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);
Thanks Maalls and dbau for the answer :).
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'