Change curl on stream_context_create - php

Pls, help me to get data with stream_context_create.
With curl work that:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, 'ecdhe_rsa_aes_128_gcm_sha_256');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$out = curl_exec($curl);
$curlError = curl_error($curl);
$result = json_decode($out, true);
curl_close($curl);
I try this:
$opts = array(
'ssl' => array(
'verify_peer' => false,
'verify_peername' => false
),
'http' => array(
'method' => 'GET',
'header' =>
'Content-Type: application/json'
)
);
$context = stream_context_create($opts);
$result = file_get_contents('$url', false, $context);
var_dump($result);
But get false. What in my code wrong?

I got the decision:
$opts = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
'http' => array(
'timeout' => 5,
'method' => 'GET',
'header' =>
'Content-type: Content-type: application/json',
'ignore_errors' => true,
'max_redirects' => 0
)
);
$context = stream_context_create($opts);
$out = file_get_contents($url, false, $context);
$result = json_decode($out, true);

Related

convert curl post to wp_remote_post

Here is curl code which is working very good
$data = array(
"to" => $to ,
"from" => $options['from_email'] ,
"subject" => $subject,
"body" => $message,
);
$url = 'https://example.com';
$api_key = 'apikeyhere'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'X-API-KEY:'.$api_key
));
$response = curl_exec($ch);
But if i try to convert this above code to worpress wp_remote_post i am getting error.
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'blocking' => true,
'sslverify' => false,
'httpversion' => '1.0',
'redirection' => 5,
'headers' => array(
'Accept: application/json',
'Content-Type: application/json',
'X-API-KEY:'.$api_key
),
'body' => json_encode($data),
) );
here is response i am getting
https://pastebin.com/Ap5LpfZb
Please let me know where i am doing wrong ?
I got it there was problem with the header array i was passing that array wrongly
Wrong code was
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'blocking' => true,
'sslverify' => false,
'httpversion' => '1.0',
'redirection' => 5,
'headers' => array(
'Accept: application/json',
'Content-Type: application/json',
'X-API-KEY:'.$api_key
),
'body' => json_encode($data),
) );
Here is correct version of code
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'blocking' => true,
'sslverify' => false,
'httpversion' => '1.0',
'redirection' => 5,
'headers' => array(
'Accept'=> 'application/json',
'Content-Type' =>'application/json',
'X-API-KEY' => $api_key,
),
'body' => json_encode($data),
) );

I'm trying to get the access token

$curl = curl_init();
$auth_data = array(
'client_id' => 'xxxxxxxx', // i have it
'client_secret' => 'xxxxxxxx', // I have it
'grant_type' => 'client_credentials'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, 'https://xxxxxx.caspio.com/oauth/token'); // I have it
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;
I would like to know why I'm getting Bad Request. When I try to show the errors it shows nothing.
This worked for me
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => "<TOKEN ENDPOINT URL>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"grant_type=client_credentials&client_id=<client_ID>&client_secret=<Secret>",
));
$response = curl_exec($curl);

Is there any function to send airtime using Reloadly API

I have tried reloadly API stated on their doc but no success, i could not find exactly the correct API end point to make Curl call.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://topups.reloadly.com/accounts/balance
");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/com.reloadly.topups-v1+json",
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik0wWXpRa"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
I got from their their API doc https://topupsapi.docs.apiary.io it stated on sending airtime but no correct endpoint stated. thank
Is there any function or correct endpoint i didn't know about?
the endpoint is https://topups.reloadly.com/topups , and it's supposed to look something like this:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://topups.reloadly.com/topups',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array(
"Accept: application/com.reloadly.topups-v1+json",
"Content-Type: application/json",
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSU",
),
CURLOPT_POSTFIELDS => json_encode(array(
'recipientPhone' => array(
'countryCode' => 'HT',
'number' => '+50936377111',
),
'senderPhone' => array(
'countryCode' => 'US',
'number' => '+13059547862',
),
'operatorId' => 173,
'amount' => 15,
'customIdentifier' => 'transaction by john#example.com',
))
));
curl_exec($ch);
curl_close($ch);
<?php
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://topups.reloadly.com/topups',
CURLOPT_POST => 1,
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/com.reloadly.topups-v1+json",
"Authorization: Bearer your_access_token")),
CURLOPT_POSTFIELDS => json_encode(array(
'recipientPhone' => array(
'countryCode' => 'HT',
'number' => '+50936377111'
),
'senderPhone' => array(
'countryCode' => 'US',
'number' => '+13059547862'
),
'operatorId' => 173,
'amount' => 15,
'customIdentifier' => 'transaction by john#example.com'
))
));
$response = curl_exec($ch);
curl_close($ch);
echo '<pre>';
var_dump($response);
echo '</pre>';
[enter image description here][1]?>

OneSignal - Add user from API

I wanna add user to OneSignal with API for push-msg. Its possible ? If yes - how to ?
thanks for answers
my code return "No Push Token" and i dont't khow how to fix this problem
<?
$fields = array(
'app_id' => "",
'language' => "ru",
'timezone' => "-28800",
'device_type' => "5",
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/players");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
$return["allresponses"] = $response;
$return = json_encode( $return);
print($return);
?>
https://documentation.onesignal.com/reference#create-notification
https://documentation.onesignal.com/reference#section-example-code-create-notification
Go through the sample code given there.
This is for Sending to all subscribers :
function sendMessage() {
$content = array(
"en" => 'English Message'
);
$hashes_array = array();
array_push($hashes_array, array(
"id" => "like-button",
"text" => "Like",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
array_push($hashes_array, array(
"id" => "like-button-2",
"text" => "Like2",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
$fields = array(
'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
'included_segments' => array(
'All'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'web_buttons' => $hashes_array
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
For specific segments:
$fields = array(
'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
'included_segments' => array('Active Users'),
'data' => array("foo" => "bar"),
'contents' => $content
);
For Onesignal player IDs:
$fields = array(
'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c",
'include_player_ids' => array("6392d91a-b206-4b7b-a620-cd68e32c3a76","76ece62b-bcfe-468c-8a78-839aeaa8c5fa","8e0f21fa-9a5a-4ae7-a9a6-ca1f24294b86"),
'data' => array("foo" => "bar"),
'contents' => $content
);

New RightSignature API "public/v1/sending_requests" throws invalid_scope error

The following is my script. I am continuously getting an invalid_scope error.
$arr = array(
'file' =>
array(
'name' => 'pdf.pdf',
'source' => 'upload',
),
'document' =>array(
'signer_sequencing' => 'false',
'expires_in' => '12',
'name' => 'Signme',
'roles' =>array(
'name' => 'a',
'signer_email' => 'example#gmail.com',
'signer_name' => 'farqalit',
),
),
'sending_request' =>array(),
);
$post_json = json_encode($arr);
$endpoint = "https://api.rightsignature.com/public/v1/sending_requests?access_token=API_TOKEN";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

Categories