I am creating a new PHP application in which I want to interact with Keybase.
First option is to use the API, but unfortunately I am running into some issues.
First step is to call the signup call.
I managed to create a curl request, but I am not sure what the pwh should look like.
I tried:
$fp = #fopen('/dev/urandom','rb');
if ($fp !== FALSE) {
$prBits .= #fread($fp,16);
#fclose($fp);
}
$salt = bin2hex($prBits);
$pwh = implode(unpack("H*",password_hash('MyP#ssword'.$prBits, PASSWORD_BCRYPT)));
But making the call results in:
{
"status": {
"code": 100,
"desc": "need a PDPKA5 key",
"name": "INPUT_ERROR"
},
"csrf_token": "lgHZIDA4ZjYyNzgy.....86aMrL09"
}
The complete call:
$data = [
"name" => "My Name",
"email" => "mymail#example.com",
"username" => "SomeUnusedUsername1234",
"pwh" => $pwh,
"pwh_version" => 3,
"salt" => $salt,
"invitation_id" => "000000001"
);
$dataString = json_encode($data);
$ch = curl_init('https://keybase.io/_/api/1.0/signup.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
Anyone got a working example or solution?
Related
I'm trying to pass some arguments in Curl but I realize that when I use test data (simple strings), I can get the result (payId and payUrl) but when I use variables instead of test data, I get nothing back.
What's the best way to troubleshoot this type of issue? It's my first time using Curl so I'm creating this function based on articles I found here.
For instance, if I replace "name" with $clientName, I won't get any return from the API.
function generatePayment($sendId, $clientCPF, $clientName, $clientEmail, $clientCep, $clientPhone, $amount){
// CONFIG
$urlCallBack = "http://192.168.0.79/sistema/admin/shipList.php?transactionStatus=success";
$urlError = "http://192.168.0.79/sistema/admin/shipList.php?transactionStatus=failed";
$debug = true;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'urlhere');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\t\"reference\": \"my-order-ref-0001\",\n\t\"client\": {\n \t\"cpf\": \"43164853858\",\n \t\"name\": \"John Doe\",\n \t\"email\": \"johndoe#example.com\",\n \t\"birthdate\": \"1982-01-14\",\n \t\"cep\": \"18053190\",\n \t\"phone\": \"15987452584\"\n\t},\n\t\"items\": [\n \t{\n \t\"reference\": \"my-item-ref-0001\",\n \t\"description\": \"Apple iMac 21.5-inch\",\n \t\"quantity\": \"1\",\n \t\"amount\": 149900\n \t},\n \t{\n \t\"reference\": \"my-item-ref-0002\",\n \t\"description\": \"Apple iPhone 11 Pro\",\n \t\"quantity\": 1,\n \t\"amount\": 99900\n \t}\n\t],\n \"coupon\": {\n \"code\": \"10DESCONTO\",\n \"value\": 1000,\n \"issuer\": \"merchant_api\"\n },\n\t\"shipping\": {\n \"amount\": 1000\n\t},\n\t\"redirect\": {\n \t \"success\": \"https://example.com/sp_callback?success\",\n \t \"failed\": \"https://example.com/sp_callback?failed\"\n\t}\n}");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$_POST['token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
if ($debug){
echo "<BR><BR><BR> DATA PASSED TO FUNCTION <BR>";
echo "<br>sendId ===> ".$sendId;
echo "<br>clientCPF ===> ".$clientCPF;
echo "<br>clientName ===> ".$clientName;
echo "<br>clientEmail ===> ".$clientEmail;
echo "<br>clientCep ===> ".$clientCep;
echo "<br>clientPhone ===> ".$clientPhone;
echo "<br>amount ===> ".$amount;
$decode = json_decode($result,true);
echo "<BR><BR> DATA RECEIVED FROM API <BR>";
echo '<br> payId generated by API ==> '.$payId = $decode['data']['order_id'];
echo '<br> PayURL generated by API ==>'.$payUrl = $decode['data']['url_checkout'];
} else {
$decode = json_decode($result,true);
$transactionId = $decode['data']['order_id'];
$payUrl = $decode['data']['url_checkout'];
$_SESSION['transactionUrl'] = $decode['data']['url_checkout'];
$_SESSION['transactionId'] = $transactionId;
$_SESSION['sendId'] = $sendId;
}
}
curl_close($ch);
// END GENERATE PAYMENT
}
Any help is appreciated on how to troubleshoot.
Updating Code using JSON
//INITIALIZE
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sandbox.splipay.com/api/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
// POST
$post = array(
"items" => array (
"description" => "Test",
"quantity" => 1,
"amount" => 10
),
"redirect" => array (
"success" => "www.success",
"failed" => "www.success"
)
);
$post = json_encode($post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// HEADERS
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$_POST['token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//EXECUTE
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
$arr = json_decode($result);
echo "<br><br>Return from VAR_DUMP";
var_dump ($arr);
echo "<BR><BR>INFO RETURN<BR>";
$info = curl_getinfo($ch);
var_dump ($info);
// $_POST['token'] = $arr['access_token'];
}
curl_close($ch);
As per documentation, these are the only values I need to pass to the API and I'd get the following response (example):
{
"success": true,
"data": {
"order_id": 57,
"url_checkout": "https://sandbox.splipay.com/payment/4openRe7Az/kzPdyP7bQr?expires=1583164527&signature=e1caae419137903f930f5a5b3d4c72608a61c0fdd36c70803d4a92c15556a4c5"
}
}
After moving to a JSON format and reviewing the API requirements, I was able to fix this. Once in JSON, it was easier to identify that there was issue with the multi dimensional array. I rewrote the code and it's working now. Thanks everyone
JSON
$post = array(
"reference" => "my-order-ref-0001",
"client" => array (
"cpf" => "43164853858",
"name" => "John Doe",
"email" => "johndoe#example.com",
"birthdate" => "1982-01-14",
"cep" => "18053190",
"phone" => "15987452584"
),
"items" => array (
array (
"reference" => "my-item-ref-0001",
"description" => "Test",
"quantity" => 1,
"amount" => 10
),
),
"coupon" => array(
"code" => "10DESCONTO",
"value" => 1000,
"issuer" => "merchant_api"
),
"shipping" => array (
"amount"=> 1000
),
"redirect" => array (
"success" => "www.success",
"failed" => "www.success"
)
);
I'm working with OpenSRS EMAIL API. I'm trying to create a new user email for a domain. In their first example, they are using JSON format :
{
"credentials": {
"user": "domain_admin#example.com",
"password": "sw0rdf1sh"
},
"user": "bhayden#example.com",
"attributes": {
"name": "Bob Hayden",
"password": "changeit",
"delivery_forward": true,
"forward_recipients": [
"bob.hayden#example.com
}
}
I'm sending this with cURL
$json = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
//
//
$payload = json_encode( $json );
//
$data = [
'Content-Type:application/json',
'X-Username:' . $connection_details['reseller_username'],
'X-Signature:' . md5(md5($payload . $connection_details['api_key']) . $connection_details['api_key']),
];
//
$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//
$response = curl_exec($ch);
//
//header('Content-type: application/json');
//
echo '<pre>';
echo $response;
echo '</pre>';
This print following error
0.9 400 0 Invalid Content-Type XCP
What am I doing wrong?
I fixed this issue by reformarting the entire request. This worked for me
$data = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
$postdata = json_encode($data);
//
$ch = curl_init($connection_details_email['api_host_port']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
I am trying to call Google Cloud Speech API by using PHP and got a problem.
$stturl = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=xxxxxxxxxxxx";
$upload = file_get_contents("1.wav");
$upload = base64_encode($upload);
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sampleRate" => 16000,
"languageCode" => "en-US"
),
"audio" => array(
"Content" => $upload,
)
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $stturl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
The result says that it is INVALID JSON PAYLOAD.
{ "error": { "code": 400, "message": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field.", "status":
"INVALID_ARGUMENT", "details": [ { "#type":
"type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ {
"field": "audio", "description": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field." } ] } ] } } "
I think this is because $upload isn't configured correctly.
According Google Cloud Speech API, it should be "A base64-encoded string".
https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
That's why I used base64_encode function, but it seems JSON doesn't process this value correctly.
Any thoughts?
You need to construct the properly formatted input as an array and then json encode it. For example, to send a file, base64encode it as "content" and submit to the API as shown:
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sample_rate" => $bitRate,
"language_code" => "en-IN"
),
"audio" => array(
"content" => base64_encode($filedata)
)
);
$data_string = json_encode($data);
$ch = curl_init($googlespeechURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
please make 'content' instead of 'Content'
small letter 'c'
its work for me.
I have alredy enable future payments permission in my app and using developer dashboard.but not working yet please find error
http://developer.paypal.com/ and log in
https://developer.paypal.com/developer/accountStatus there you can see what permits you have.
$data = array(
"intent" => "authorize",
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(
array("amount" => array(
"currency" => "USD",
"total" => "1.88"
),
"description" => "future of sauces")
));
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
$headers = array(
'Content-Type: application/json',
'PayPal-Client-Metadata-Id: d6743cbb53ea4470a53bfe458f0cb885',
'Authorization: Bearer A103.B7d5318JDS6NA1zGh02avjCx16oxnBPadUat5z9SlGVGEiOhoAeMuqyz0anGSICS.FAkzECypTS1IXfemHcpVa5yyrGu',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
$information = curl_getinfo($ch);
curl_close($ch);
print_r($information);
die;
Out put here
{"name":"PERMISSION_DENIED","message":"No permission for the requested operation","information_link":"https://developer.paypal.com/docs/api/#PERMISSION_DENIED","debug_id":"5b39efd4cf370"}Array
(
[url] => https://api.sandbox.paypal.com/v1/payments/payment
[content_type] => application/json
[http_code] => 403
[header_size] => 592
I made a Paypal Module, hope this code would help you.
$result_json = json_decode($result);
/* Check if authentication is valid */
if (isset($result_json->access_token))
{
$enc_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/payments/payment');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, !in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $enc_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$result_json->access_token,
'PayPal-Client-Metadata-Id: ************'
));
$result = curl_exec($ch);
$json_result = json_decode($result);
curl_close($ch);
}
json_encode($data) there are additional information that may not be useful for the transaction you might trying to do but it is an example.
{
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"payer_info": {
"email": "...",
"shipping_address": {
[...]
}
},
"funding_instruments": [
{
"credit_card": {
[...]
}
}
}
]
},
"transactions": [
{
"amount": {
"total": 32.91,
"currency": "USD"
},
"item_list": {
"items": [
{
"quantity": 1,
"name": "Product Name",
"price": 16.51,
"currency": "USD"
},
{
"quantity": 1,
"name": "Product Name 2",
"price": "16.40",
"currency": "USD"
},
{
"quantity": 1,
"name": "Shipping",
"price": 0,
"currency": "USD"
}
],
"shipping_address": {
[...]
}
}
}
]
}
As I didn't get any solution from anybody so I dug into my code step by step and found solution.
function paypalFuturePayment($userID,$amount)
{
$amount=number_format($amount,2);
/* paypal App truxx dev client and secret key*/
if($userID && $amount){
$userData = selectById('tbl_users','*',"id='".$userID."'");
$refresh_token = $userData['paypal_refresh_tokens'];
$Metadata_id = $userData['paypal_metadata_id'];
if($refresh_token && $Metadata_id){
if ($_SERVER['SERVER_NAME'] == 'syonserver.com') {
$clientId = "xxxxx";
$secret = "xxx";
$url1="https://api.sandbox.paypal.com/v1/oauth2/token";
$url2="https://api.sandbox.paypal.com/v1/payments/payment";
}else{
$account = 0; // 0 for sandbox ,1 for live
if ($account == 1) {
//client live
$clientId = "xxx";
$secret = xxx";
$url1 = "https://api.paypal.com/v1/oauth2/token";
$url2 = "https://api.paypal.com/v1/payments/payment";
} else {
//client sandbox
$clientId = "xxx";
$secret = "xxx";
$url1 = "https://api.sandbox.paypal.com/v1/oauth2/token";
$url2 = "https://api.sandbox.paypal.com/v1/payments/payment";
}
}
//print_r($refresh_token);die;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded");
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&refresh_token=" . $refresh_token);
$result = curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
//11111111 for payment Authorize: For example, to first authorize the payment, use a request similar to this:
$access_token = $result->access_token;
$data = array(
"intent" => "authorize",
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(
array("amount" => array(
"currency" => "USD",
"total" => $amount
),
"description" => "future of sauces")
));
$data_string = json_encode($data);
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url2);
$headers = array(
'Content-Type: application/json',
'PayPal-Client-Metadata-Id: '.$Metadata_id,
'Authorization: Bearer '.$access_token,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data_string);
$result1 = curl_exec($ch1);
curl_close($ch1);
$result1=json_decode($result1);
$message = $result1->message;
if($result1->state=='approved'){
$access_id= $result1->transactions[0]->related_resources[0]->authorization->id;
}else{
if(empty($message)){
$message ='Authorization error, Please try again.';
}
return array('response' => '', 'success' => '0','message'=>$message);
}
// print_r($result1);die;
//2222222 capture the payment:
$data = array("amount" => array(
"currency" => "USD",
"total" => $amount
),
"is_final_capture" => "true"
);
$data_string = json_encode($data);
$ch2 = curl_init();
if ($_SERVER['SERVER_NAME'] == 'syonserver.com') {
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/authorization/$access_id/capture");
}else {
$account = 0; // 0 for sandbox ,1 for live
if ($account == 1) {
//client live
curl_setopt($ch2, CURLOPT_URL, "https://api.paypal.com/v1/payments/authorization/$access_id/capture");
}else{
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/authorization/$access_id/capture");
}
}
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$access_token,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($ch2);
curl_close($ch2);
$response_a = json_decode($response, true);
$state = $response_a['state'];
$message = $response_a['message'];
if(!empty($response_a)){
if($state=='completed') {
return array('response' => $response_a, 'success' => '1','message'=>'Data received');
}else{
if(empty($message)){
$message ='Payment authorization error, Please try again.';
}
return array('response' => '', 'success' => '0','message'=>$message);
}
}
else{
return array('response' => '','success'=>'0','message'=>'Response nil');
}
}
else
{
return array('response' => '', 'success' => '0','message'=>'Authorization code not available.');
}
}else{
return array('response' => '', 'success' => '0','message'=>'Unauthorize request.');
}
}
My code is here
Please see carefully
IOs gcm not working in background only working in foreground php script
$data = array('message' =>$msg,
'contentTitle' => $msg,
"tickerText" => "some ticker text",
"contentTitle" => "some content title",
"contentText" => "some content description text",
"sound"=> "default",
"vibrate" => "false",
"otherCustomData" => "some value"
);
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message" => $data),
'content_available' => true
);
//have to change
$headers = array(
'Authorization: key=IDSSSSSSS',
'Content-Type: application/json'
);
}
$ch = curl_init();
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);
// print_r($result);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
My Ios developer said to me its only working in foreground not working in background.This is properly working in android
Please help fast
Thanks in advance
You need to put notification parameter into your message. This way, GCM will deliver your message over APNS. Or, use content_available.
https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support