Preparing a Callback URL for Indodax API using PHP - php

I am trying to make a php script to communicate with the indodax API endpoint. from the documentation provided there are some line that make me confused, namely in the following sentence: ( https://github.com/btcid/indodax-official-api-docs/blob/master/Private-RestAPI.md#withdraw-coin-endpoints )
"You also need to prepare a Callback URL. Callback URL is a URL that our system will call to verify your withdrawal requests. Various parameters will be sent to Callback URL, make sure to check this information on your server side. If all the data is correct, print out a string “ok” (without quotes). We will continue the request if only we receive “ok” (without quotes) response, otherwise the request will be failed."
Questions:
If I write a php script at https://www.ok.com/withdraw.php , then the URLCallback is https://www.ok.com/withdraw.php ? true or false ?
from the API document above :
"If all the data is correct, print out a string “ok” (without quotes). We will continue the request if only we receive “ok” (without quotes) response, otherwise the request will be failed."
what is "ok" (without quotes) response? does it have to be written in php script or not? if yes, please help me complete this code.
<?php
$url = 'https://indodax.com/tapi';
// Please find Key from trade API Indodax exchange
$key = '****';
// Please find Secret Key from trade API Indodax exchange
$secretKey = '****';
$data = [
'method' => 'withdrawCoin',
'timestamp' => '1578304294000',
'recvWindow' => '1578303937000',
'currency' => 'doge',
'withdraw_address' => 'D9iCdBLBosJzGSvpQGMSobwtdgB2rS1zam',
'withdraw_amount' => '10',
'withdraw_memo' => 'memo',
'request_id' => 'trx002'
];
$post_data = http_build_query($data, '', '&');
$sign = hash_hmac('sha512', $post_data, $secretKey);
$headers = ['Key:'.$key,'Sign:'.$sign];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I've tried to make a php script as above with the api key and secret key ( https://github.com/btcid/indodax-official-api-docs/blob/master/example/Private-RestAPI-php.md#withdraw-coin ) . then the result is
"{"success":0,"error":"did not pass callback check","request_error":"","callback_response":"error code: 1020"}
"

Related

Trying to send a HTTP request but have no idea how to do it

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.

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.

PHP: 500 Server Error - Request for access token for Microsoft Azure Active Directory Access

I'm following this documentation: https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code
I have just completed this step (by simply visiting the URL below):
https://login.microsoftonline.com/{tenant}/oauth2/authorize?client_id={client_id}&response_type=code
Which redirected me to my redirect URL with code query string attached:
https://example.com/?code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCm.................................
At the end of the code query string above, there was an additional session_state parameter.
The next step outlined in the link at the top of this question says to make a POST request, but I'm having trouble forming this call.
Here's what the docs represent being an example:
How would I form and call this request in PHP (without using cURL)?
Here's my attempt, but I don't know whether or not I'm correct:
$url = 'https://login.microsoftonline.com/{tenant}/oath2/token';
$data = array( 'grant_type' => 'authorization_code',
'client_id' => '2d4d11a2-f814-46a7-890a-274a72a7309e',
'code' => 'AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCm...................',
'redirect_uri' => 'https://example.com',
'resource' => 'https://graph.microsoft.com',
'client_secret' => '{client_secret}' );
$options = array(
'http' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$var_dump($result);
UPDATE: The code above (when executed) returns a 500 Internal Server Error.
Also, I don't know whether or not I should be adding session_state (mentioned above) into the POST call.
The URL should be https://login.microsoftonline.com/{tenant}/oauth2/token.
You have to add the client secret to the request, as well as the resource.
If you are trying to call Microsoft Graph for example, the resource should be https://graph.microsoft.com.

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);

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