Trouble Sending transaction using PHP curl and TRON api - php

I am playing around with the TRON api and trying to send transactions using the PHP curl method. I can successfully send the transaction in the TRON Developer sandbox but get and error when executing it on my server.
Error im receiving -
status_code: 200
{"result": {"code": "CONTRACT_VALIDATE_ERROR","message": "313a39343a20494e56414c49442068657820537472696e67"}}
My php Curl request -
$ch = curl_init();
$headers = [
'Content-Type: application/json'
];
$postData = [
'privateKey' => 'MY Private Key',
'toAddress' => 'To Address',
'amount' => '100000'
];
curl_setopt($ch, CURLOPT_URL,"https://api.trongrid.io/wallet/easytransferbyprivate");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $statusCode;
print_r($result);
Here is the link to the TRON sandbox im using -
https://developers.tron.network/reference#east-transfer-by-private-key
The success code in the sandbox is 200 so not sure why im receiving that and I am not sure how to lookup the returned error message, any idea what im doing wrong?
This is the sample post request given from TRON -
curl -X POST https://api.trongrid.io/wallet/easytransferbyprivate -d '{"privateKey": "D95611A9AF2A2A45359106222ED1AFED48853D9A44DEFF8DC7913F5CBA727366", "toAddress":"4112E621D5577311998708F4D7B9F71F86DAE138B5","amount":10000}'

I figured it out, I was preparing the data wrong..
Instead creating an array -
$postData = [
'privateKey' => 'MY Private Key',
'toAddress' => 'To Address',
'amount' => '100000'
];
I needed to do the literal string and not encode it.
$postData = '{"privateKey": "xxx", "toAddress..etc
Just like in the example given in the sandbox.

Related

Send informations via Firebase Cloud Messaging

I am currently trying to make use of cloud messaging for my Android app. The server is sending messages via PHP using php-curl.
The problem is that the server's response is always:
401: Unauthorized
I use the WebAPI Access key from the Firebase Console for my App, so this is definitively the right one. Below is the code I use to send the data:
<?php
$fields = array(
'to' => "<MY-RECIPIENT-TOKEN>",
'notification' => array(
'body' => 'Test message :)',
'title' => 'Test',
'icon' => 'myicon',
'sound' => 'mySound'
)
);
$headers = array(
'Authorization:key=<MY AUTH KEY IS HERE>',
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
The MY-RECIPIENT-TOKEN is received from the App and the MY AUTH KEY IS HERE is the key from the Firebase Console.
What am I missing here?
Nevermind. The Problem is not in the code, the code is fine. If someone stumbles upon this: There are 2 keys in your FCM Console. When you go to your Project Settings you will find the Web-API-Key. This is not the key you are looking for! You need to go to Settings -> Cloud Messaging. There you will see a much longer Server key. THIS is the key you want to use!

Firebase Cloud Messaging - PHP Rest API not working for iOS

I am using the PHP code given below to send notification with custom payload through Firebase Cloud Messaging. It is working for Android but not for the iOS.
However, I am able to receive notification sent from firebase cloud messaging console. Kindly advise.
public function send_notification($registatoin_ids, $message, $title, $sound,
$purpose)
{
// Set POST variables
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => array(
"for" => $purpose,
"id" => strtotime("now"),
"message" => $message,
"title" => $title,
"sound" => $sound,
"vibrate" => 1,
"date" => date("D d, M Y h:i A"),
"priority"=>'high',
"content_available"=>false
),
'time_to_live' => 600,
);
$headers = array(
'Authorization: key=' . FCM_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
return $result;
}
Android and iOS handle Push Notifications differently. While Android will only wake from background when only the data tag is provided, iOS devices require the notification tag to deal with notifications received while the app is in the background.

Neteller TransferOut with PHP / CURL

I'm having some problems using the Neteller API to transfer money out of our merchant account to a user. I've succcessfully received the accessToken, however when I try using transferOut I just get invalid credentials? The code I'm using is:
$headers = array(
"Content-type" => "application/json",
"Authorization" => "Bearer " . $accessToken
);
//build the request body structure
$requestParams = array(
"payeeProfile" => array(
"email" => $the_email_address_to_send_to
),
"transaction" => array(
"merchantRefId" => $transaction_id,
"amount" => $amount,
"currency" => $currencyCode
)
);
// encode the requestParams to a string
$requestParams = json_encode($requestParams);
// The curl stuff
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://api.neteller.com/v1/transferOut");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestParams);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Ok lets send this lovely looking curl over
$serverOutput = json_decode(curl_exec($curl));
Obviously all variables ($transaction_id, $amount, $currency) are set appropriately. However the response I get back is:
stdClass Object
(
[error] => stdClass Object
(
[code] => 5279
[message] => Authentication credentials are invalid
)
)
I'm confused, surely the accessToken is the credentials I need, and theyve already been received. Am I meant to include anything else in the transferOut curl postfields?
Thanks in advance
As per user3584460's comment:
$headers does not look OK - try $headers = array("Content-type: application/json", "Authorization: Bearer " . $accessToken);. At least this is the format according to http://php.net/manual/en/function.curl-setopt.php
Note, the Merchant Ref ID also needs to be a certain length. unsure what - can't find reference, but 8 characters is not long enough.

Php curl json post returns error

I am trying a little script to create and update domain names using Digital Ocean api v2 here - https://developers.digitalocean.com/documentation/v2/#update-a-domain-record
For some reason the response from the server is that I am missing record type.
Here is my code:
$headers = array(
'Content-Type:application/json',
'Authorization: Bearer ' . file_get_contents('token.txt') );
$rawdata = array(
'type' => 'A',
'name' => 'sub',
'data' => '162.10.66.0',
'priority' => null,
'port' => null,
'weight' => null
);
$postdata = json_encode($rawdata);
$ch = curl_init(); // initiate curl
$url = "https://api.digitalocean.com/v2/domains/mydomain.org/records";
curl_setopt($ch, CURLOPTURL,$url);
curl_setopt($ch, CURLOPTPOST, true);
curl_setopt($ch, CURLOPTPOSTFIELDS, $postdata);
curl_setopt($ch, CURLOPTRETURNTRANSFER, true);
curl_setopt($ch, CURLOPTHTTPHEADER, $headers);
$output = curlexec ($ch); // execute
curl_close ($ch); // close curl handle
vardump($output); // show output
This is the error
string(81) "{"id":"unprocessable_entity","message":"Record type is not included in the list"}"
What am I missing here?
It's not a PHP error, it's an error return by the webservice you're calling. Check their documentation to see if you're sending the right stuff to them.

paypal rest api payment call returning 401 using php

I am trying to add paypal to my site. I have been following the instructions at https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/, but it is not working. I have sent the request for an access token successfully and gotten a response. The information in the response is stored in an object called $accessToken. The problem lies when I try to make the API call in step 3 from the site listed above. I get a 401 error sent back from the request. I'm pretty sure the $url that the request is sent to as a function parameter is correct. It is https://api.sandbox.paypal.com/v1/payments/payment. I have been going all over the internet for help for the past week and a half, and I haven't made any progress whatsoever. Any help would be greatly appreciated. Thanks!
function MakePaymentAPICall($accessToken, $sale, $url, $url_success, $url_cancel){
// Create cURL resource
$ch = curl_init();
// Set url
curl_setopt($ch, CURLOPT_URL, $url);
$tokenType = $accessToken->GetTokenType();
$token = $accessToken->GetAccessToken();
$auth = "Authorization:" . $tokenType . " " . $token;
$saleTotal = $sale->GetTotal();
$header = array(
'Content-Type' => 'application/json',
'Authorization' => $tokenType . ' ' . $token
);
$dataArray = array(
'intent' => 'sale',
'redirect_urls' => array(
'return_url' => $url_success,
'cancel_url' => $url_cancel
),
'payer' => array(
'payment_method' => 'paypal'
),
'transactions' => array(
'amount' => array(
'total' => $saleTotal,
'currency' => 'USD'
),
'description' => 'Test payment.'
)
);
curl_setopt($ch, CURLOPT_HEADER, http_build_query($header));
// set data to post
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataArray));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
// Execute curl command
$output = curl_exec($ch);
// Get info about request
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL resource to free up system resources
curl_close($ch);
return $output;
} // MakePaymentAPICall function
#Jason247
http://php.net/manual/en/function.curl-setopt.php
I think that CURLOPT_HEADER requires an int or bool, either 1 or TRUE to send headers.
I do believe CURLOP_HTTPHEADER is what you want, you can pass an array directly to it without encoding to a query string.
e.g.
curl_setopt($curlHandle, CURLOPT_HEADER, 1);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);
A 401 error generally indicates that the access token is either invalid or expired:
https://developer.paypal.com/webapps/developer/docs/integration/direct/rest-payments-error-handling/
Are you including "Bearer" in the Authorization header? Example:
Authorization:Bearer EMxItHE7Zl4cMdkvMg-f7c63GQgYZU8FjyPWKQlpsqQP

Categories