Files
wp-strava/lib/API.class.php
T

88 lines
2.3 KiB
PHP
Raw Normal View History

2013-03-31 21:28:49 -05:00
<?php
/*
* Util is a class with all the utility methods.
*/
class WPStrava_API {
2014-09-10 23:35:08 -05:00
//deactivated
//const STRAVA_V1_API = 'http://www.strava.com/api/v1/'; //rides?athleteId=134698
//const STRAVA_V2_API = 'http://www.strava.com/api/v2/'; //rides/:ride_id/map_details
const STRAVA_V3_API = 'https://www.strava.com/api/v3/';
2013-03-31 21:28:49 -05:00
2014-09-10 23:35:08 -05:00
public function __construct( $access_token ) {
$this->access_token = $access_token;
}
2017-05-26 10:53:24 -05:00
public function post( $uri, $data = null ) {
2014-09-10 23:35:08 -05:00
$url = self::STRAVA_V3_API;
2013-03-31 21:28:49 -05:00
$args = array(
'body' => http_build_query( $data ),
2014-09-10 23:35:08 -05:00
'sslverify' => false,
'headers' => array(
'Authorization' => 'Bearer ' . $this->access_token,
2014-09-10 23:35:08 -05:00
)
2013-03-31 21:28:49 -05:00
);
$response = wp_remote_post( $url . $uri, $args );
2015-05-11 22:28:33 -05:00
if ( is_wp_error( $response ) )
return $response;
2013-04-07 17:56:07 -05:00
if ( $response['response']['code'] != 200 ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
$error = $body->error;
else
$error = print_r( $response, true );
return new WP_Error( 'wp-strava_post',
sprintf( __( 'ERROR %s %s - %s', 'wp-strava'), $response['response']['code'], $response['response']['message'], $error ),
$response );
2013-03-31 21:28:49 -05:00
}
return json_decode( $response['body'] );
}
2017-05-26 10:53:24 -05:00
public function get( $uri, $args = null ) {
2014-09-10 23:35:08 -05:00
$url = self::STRAVA_V3_API;
2013-03-31 21:28:49 -05:00
2013-04-07 17:56:07 -05:00
$url .= $uri;
if ( ! empty( $args ) )
$url = add_query_arg( $args, $url );
2014-09-10 23:35:08 -05:00
$get_args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $this->access_token,
2014-09-10 23:35:08 -05:00
)
);
2017-05-26 10:53:24 -05:00
2014-09-10 23:35:08 -05:00
$response = wp_remote_get( $url, $get_args );
2013-04-07 17:56:07 -05:00
if ( is_wp_error( $response ) )
return $response;
2017-05-26 10:53:24 -05:00
2013-04-07 17:56:07 -05:00
if ( $response['response']['code'] != 200 ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
$error = $body->error;
2014-09-23 13:39:48 -05:00
else if ( $response['response']['code'] == 503 )
$error = __( 'Strava Temporarily Unavailable', 'wp-strava' );
2013-04-07 17:56:07 -05:00
else
$error = print_r( $response, true );
2013-03-31 21:28:49 -05:00
2013-04-07 17:56:07 -05:00
return new WP_Error( 'wp-strava_get',
2017-05-26 10:53:24 -05:00
sprintf( __( 'ERROR %s %s - %s', 'wp-strava' ), $response['response']['code'], $response['response']['message'], $error ),
2013-04-07 17:56:07 -05:00
$response );
2013-03-31 21:28:49 -05:00
}
return json_decode( $response['body'] );
}
2017-05-26 10:53:24 -05:00
} // Class API.