Start a new topic

PHP REST API Wrapper

Hi there,


is there any developer which could also need a PHP wrapper for the OST REST API? If yes please reply so we can save some time together.


Regards

Tobias


Yes my OST KIT Alpha Phase II Project could use this.  Also I'm in need of a senior Buddypress developer that is familiar with OST in case you might have some leads.

No sorry. I was playing around with Buddypress a while ago... but I didnt really like it. Too less options to hook etc. And being forced to fork is not a nice way to develop an extension. ;-) 

Thanks for your input.  Will you be developing the PHP wrapper?  Also what WP social network plugin would you suggest?

I didn't do any research on this since 6 month. But buddypress is probably the way to go if you use wordpress and it fits to your requirements. Don't know if there is a good commercial plugin available.


Yes or I will try out how good the REST description is working. Maybe I can also generate it. Will see.

Depend of if OST can implement something like https://swagger.io/specification/ or https://github.com/OAI/OpenAPI-Specification.


And yes I will publish it on github then :)

Hi ,


Did you already implement it? Will you publish it on github? or else I need to implement php wrapper for  OST rest api. 


Thanks




Hi Durga, no not yet. I would have started in 2-3 days with this task.

Hi,


Did you finish the coding part related to hash_hmac, secret key or apikey is input to this function? It would be helpful if you can post the code snippet. Thanks.



Yes but not completly tested. You can try it out:


 

	/**
	 * Builds and signs the query and add all needed parameters (api_key, request_timestamp, signature)
	 *
	 * @param string $endpoint
	 * @param array $parameters
	 * @param string $method 'get' or 'post'
	 *
	 * @return string|string[]|null
	 * @throws \Exception
	 */
	protected function buildQueryData($endpoint, $parameters, $method = 'post') {

		$method = strtolower($method);

		$parameters['api_key'] = $this->api_key;
		$parameters['request_timestamp'] = time();
		ksort($parameters);

		$query_params = array_map ( function($val, $key) {
			if(is_array($val)) {
				$key .= '[]';
				$val = implode(strtolower($key) . '=', $val);
			}
			return strtolower($key) . '=' . urlencode($val);
		}, $parameters, array_keys($parameters) );

		$query = $endpoint . '?' . implode('&', $query_params);

		$signature = hash_hmac ('sha256', $query, $this->api_secret);

		if($method == 'get') {
			return $query . '&signature='.$signature;
		} elseif($method == 'post') {
			$query_params['signature'] = $signature;
			return $query_params;
		} else {
			throw new \Exception('Wrong method type while building query data');
		}
	}

 


1 person likes this

@TechBusiness I'm doing the programming on Mike's project, thanks for doing the tricky part of the PHP API class!


Just for the array_map part, I couldn't get my head around whether it would always work as intended so I did a rewrite and this test: 

$parameters = array('name' => 'something', 'uuids' => array('http://', '1', '98safds3'), 'blah' => 'test123');
echo PHP_EOL."Input Parameter Array".PHP_EOL.print_r($parameters, true);

// Method A
$query_params = array_map(function($val, $key) {
	if (is_array($val)) {
		$key .= '[]';
		$val = implode(strtolower($key) . '=', $val);
	}
	return strtolower($key) . '=' . urlencode($val);
}, $parameters, array_keys($parameters) );
$query = implode('&', $query_params);
echo PHP_EOL."TEST METHOD A".PHP_EOL.print_r($query_params, true).PHP_EOL.$query;

// Method B
foreach ($parameters as $key => $value) {
	if (is_array($value)) {
		foreach ($value as $val) {$query_params2[] = strtolower($key).'[]='.urlencode($val);}
	} else {$query_params2[] = strtolower($key).'='.urlencode($value);}
}
$query2 = implode('&', $query_params2);
echo PHP_EOL."TEST METHOD B".PHP_EOL.print_r($query_params2, true).PHP_EOL.$query2; 

I find the simpler method of looping is more reliable and easier to understand, though its rather academic since it only seems like it will affect the /transaction-types/status endpoint for querying multiple transaction_uuids... but still it does seem to give a better result, can you confirm? 


1 person likes this

Not sure if it is still needed, but this repo may help https://github.com/danieldeloretta/PHP-OSTKit


1 person likes this

Guess there is a bit of overlap going on here, a few of us now writing this...

Here is my completed version, based on the signature code above...

I tried to make it do everything it could and included a few tweaks from the others:

https://github.com/majick777/php-ost-kit

Ha! Indeed, anyway.. lots of PHP implementations to choose from. Here is another one: https://github.com/realJayNay/ost-kit-php-client.


Maybe we should be a little proactive next time and develop one together :) GitHub is all about contributing and giving back to the open source community. We might get more done more efficiently this way. Just my two cents. Happy hacking!

Maybe a good moment now when the new API 1.0 comes out?


1 person likes this

Thanks.  My developer Tony Hayes has written our PHP wrapper.  I believe he has posted a github link on this thread also.

Did anyone implement PHP wrapper for V1? 

Login or Signup to post a comment