Has anyone managed to access pritunl's api using php?
The guide provided from the source code https://github.com/pritunl/pritunl/blob/master/tools/test_pritunl.py has only python examples.
If I try do read data, it works fine. But when I try to update or create a new entity I receive HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
My code is:
public static function auth_request($method, $path, $data="")
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
if ($data) {
array_push($auth_array, $data);
}
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
Solved removing the $data from $auth_array.
This is my new code:
public static function auth_request($method, $path, $data="") {
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
}
Related
I am using 000webhosting.com. I have uploaded a file check.php
<?php
$url = 'http://www.otherdomain.com/login/?';
$data = array('user' => 'admin',
'pass' => '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);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
?>
When i call www.000webhosting.com/check.php, it returns string(0) ""
Whereas , when i upload check.php in allcodez.com and call www.allcodez.com/check.php, it returns {"status":"true", "login":"true"}.
Is the problem with 000webhosting.com settings? or any other?
$key = 'fd2fdfsdfsdfdsf';
$secret = 'fdsfdsfdsfdsfdsfsdf';
$basic_credentials = base64_encode($key.':'.$secret);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Authorization: Basic '.$basic_credentials."\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
'content' => 'grant_type=client_credentials'
)
);
$context = stream_context_create($opts);
$pre_token = file_get_contents('https://api.twitter.com/oauth2/token', false, $context);
$token = json_decode($pre_token, true);
if (isset($token["token_type"]) && $token["token_type"] == "bearer")
{
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$token["access_token"]
)
);
$context = stream_context_create($opts);
$data = file_get_contents('https://api.twitter.com/1.1/search/users.json?q=twitter', false, $context);
var_dump($data); die();
The code above returns false. I have no idea why. When I change the url into:
https://api.twitter.com/1.1/statuses/user_timeline.json?count=200&user_id=43243243
It works perfectly and returns the whole JSON.
Why is that so? I can't get it to work with any other API methods other than this one. The key and secret are the same in each situation. Nothing should be changed.
I am trying to implement PayPal Adaptive Payments, but as soon as I try to post with cURL, I get a NULL response and curl_error() gives the following error:
0NSS: client certificate not found (nickname not specified)
Here is the full PHP code, I am using sandbox. Some sensitive info is starred (*).
<?php
class PayPal {
public $API_USER = "*********.gmail.com";
public $API_PASSWORD = "********************";
public $API_SIGNATURE = "******************************";
public $API_APP_ID = "APP-80W284485P519543T";
public $apiUrl = "https://svcs.sandbox.paypal.com/AdaptivePayments/";
public $paypalUrl = "https://sandbox.paypal.com/webscr?cmd=ap-payment&paykey=";
public $headers = array();
function __construct() {
$this->headers = array(
"X-PAYPAL-SECURITY-USERID : " . $this->API_USER,
"X-PAYPAL-SECURITY-PASSWORD : " . $this->API_PASSWORD,
"X-PAYPAL-SECURITY-SIGNATURE : " . $this->API_SIGNATURE,
"X-PAYPAL-REQUEST-DATA-FORMAT : JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT : JSON",
"X-PAYPAL-APPLICATION-ID : " . $this->API_APP_ID
);
}
function paypalSend($data, $call) {
$ch = curl_init();
$curl_params = array(
CURLOPT_URL => $this->apiUrl . $call,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_POSTFIELDS => json_encode($data)
);
curl_setopt_array($ch, $curl_params);
$result = curl_exec($ch);
echo(curl_errno($ch));
echo(curl_error($ch));
curl_close($ch);
return json_decode($result, TRUE);
}
function getPayKey() {
$createPacket = array(
"actionType" => "PAY",
"currencyCode" => "USD",
"receiverList" => array(
"receiver" => array(
array(
"amount" => "5.00",
"email" => "*********#gmail.com"
)
)
),
"returnUrl" => "http://example.com/success",
"cancelUrl" => "http://example.com/failure",
"requestEnvelope" => array(
"errorLanguage" => "en_US",
"detailLevel" => "ReturnAll"
)
);
// paypal Send
$response = $this->paypalSend($createPacket, "Pay");
var_dump($response);
}
}
$pp = new Paypal();
$pp->getPayKey();
?>
I've set up a PHP script to send a text message using the Nexmo API, however there's a problem with how I'm encoding the HTTP GET request. The script pulls some variables from stored data, and then fires. I'm not experienced with PHP and have spent so much time on this already, this is my last resort, is anyone able to see where I've gone wrong in the following code?
The intended format for the request:
https://rest.nexmo.com/sms/json?api_key=XXX&api_secret=XXX&to=XXX&from=XXX&text=XXX
The code:
function sendText () {
$api_key = $settings['consumer_key'];
$api_secret = $settings['consumer_secret'];
$recipient = $settings['recipient'];
$from = $settings['from'];
$text = $settings['sms_contents'];
$url = 'https://rest.nexmo.com/sms/json';
$data = array_merge($data, array('api_key' => $api_key, 'api_secret' => $api_secret, 'to' => $recipient, 'from' => $from, 'text' => $text));
$post = '';
foreach($data as $k => $v) {
$post .= "&$k=$v";
}
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post
)
);
$context = stream_context_create($opts);
$from_nexmo = file_get_contents($url, false, $context);
}
sendText();
I don’t think you are doing any encoding. Here is what I would do:
foreach($data as $k => $v){
$post .= "&$k=" . urlencode($v);
}
Try to use urlencode() for transferred values:
$post = array();
foreach($data as $k => $v){
$post[] = $k . '=' . urlencode($v);
}
$post = implode('&', $post);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post
)
);
Please try executing the following code snippet:
function sendText () {
$api_key = $settings['consumer_key'];
$api_secret = $settings['consumer_secret'];
$recipient = $settings['recipient'];
$from = $settings['from'];
$text = $settings['sms_contents'];
$url = 'https://rest.nexmo.com/sms/json';
$data = array_merge($data, array('api_key' => $api_key, 'api_secret' => $api_secret, 'to' => $recipient, 'from' => $from, 'text' => $text));
$post = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post
)
);
$context = stream_context_create($opts);
$from_nexmo = file_get_contents($url, false, $context);
}
sendText();
I'm trying to use this nice function:
function do_post_request($url, $data, $optional_headers = null) {
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
to send a POST command to a specific url, my problem is i'm trying to send the post paramters in a form of an array, something like this
login[username]: myusername
login[password]: mypassword,
however i'm not able to do that, calling the function with :
$login_post = array('login[username]' => $email,
'login[password]' => '');
do_post_request(getHost($website['code']), $login_post);
always send the data to the post in the form:
username: myusername
password: mypassword
How can I avoid this (without using curl)? Thanks a lot.
Thanks
Yehia
Maybe with that ?
<?php
// Define POST data
$donnees = array(
'login' => 'test',
'password' => '******' );
function http_build_headers( $headers ) {
$headers_brut = '';
foreach( $headers as $name => $value ) {
$headers_brut .= $name . ': ' . $value . "\r\n";
}
return $headers_brut;
}
$content = http_build_query( $donnees );
// define headers
$headers = http_build_headers( array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Length' => strlen( $content) ) );
// Define context
$options = array( 'http' => array( 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0',
'method' => 'POST',
'content' => $content,
'header' => $headers ) );
// Create context
$contexte = stream_context_create( $options );
// Send request
$return = file_get_contents( 'http://www.exemple.com', false, $contexte );
?>
$login_post = array(
'login' => array(
'username' => $email,
'password' => ''))
Try using stream_context_create
$url = 'http://your_url.com/path';
$data = array('login' => 'usrnname', 'password' => '**');
$opt = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($opt);
$result = file_get_contents($url, false, $context);
var_dump($result);
This method doesnt use curl.