@@ -549,22 +549,23 @@ protected function _nonce($length = 8)
549
549
/**
550
550
* Generates an OAuth signature
551
551
*
552
- * @param string $httpmethod Usually either 'GET' or 'POST' or 'DELETE'
553
- * @param string $method The API method to call
554
- * @param array optional $params The API call parameters, associative
552
+ * @param string $httpmethod Usually either 'GET' or 'POST' or 'DELETE'
553
+ * @param string $method The API method to call
554
+ * @param array optional $params The API call parameters, associative
555
+ * @param bool optional append_to_get Whether to append the OAuth params to GET
555
556
*
556
557
* @return string Authorization HTTP header
557
558
*/
558
- protected function _sign ($ httpmethod , $ method , $ params = array ())
559
+ protected function _sign ($ httpmethod , $ method , $ params = array (), $ append_to_get = false )
559
560
{
560
561
if (self ::$ _oauth_consumer_key === null ) {
561
562
throw new \Exception ('To generate a signature, the consumer key must be set. ' );
562
563
}
563
564
$ sign_params = array (
564
- 'consumer_key ' => self ::$ _oauth_consumer_key ,
565
- 'version ' => '1.0 ' ,
566
- 'timestamp ' => time (),
567
- 'nonce ' => $ this ->_nonce (),
565
+ 'consumer_key ' => self ::$ _oauth_consumer_key ,
566
+ 'version ' => '1.0 ' ,
567
+ 'timestamp ' => time (),
568
+ 'nonce ' => $ this ->_nonce (),
568
569
'signature_method ' => 'HMAC-SHA1 '
569
570
);
570
571
$ sign_base_params = array ();
@@ -586,13 +587,20 @@ protected function _sign($httpmethod, $method, $params = array())
586
587
$ sign_base_string = substr ($ sign_base_string , 0 , -1 );
587
588
$ signature = $ this ->_sha1 ($ httpmethod . '& ' . $ this ->_url ($ method ) . '& ' . $ this ->_url ($ sign_base_string ));
588
589
589
- $ params = array_merge ($ oauth_params , array (
590
- 'oauth_signature ' => $ signature
591
- ));
592
- ksort ($ params );
593
- $ authorization = 'Authorization: OAuth ' ;
594
- foreach ($ params as $ key => $ value ) {
595
- $ authorization .= $ key . '=" ' . $ this ->_url ($ value ) . '", ' ;
590
+ $ params = $ append_to_get ? $ sign_base_params : $ oauth_params ;
591
+ $ params ['oauth_signature ' ] = $ signature ;
592
+ $ keys = $ params ;
593
+ ksort ($ keys );
594
+ if ($ append_to_get ) {
595
+ $ authorization = '' ;
596
+ foreach ($ keys as $ key => $ value ) {
597
+ $ authorization .= $ key . '=" ' . $ this ->_url ($ value ) . '", ' ;
598
+ }
599
+ return authorization.substring (0 , authorization.length - 1 );
600
+ }
601
+ $ authorization = 'OAuth ' ;
602
+ foreach ($ keys as $ key => $ value ) {
603
+ $ authorization .= $ key . "= \"" . $ this ->_url ($ value ) . "\", " ;
596
604
}
597
605
return substr ($ authorization , 0 , -2 );
598
606
}
@@ -1007,49 +1015,49 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
1007
1015
$ params ['application_id ' ] = 333903271 ;
1008
1016
}
1009
1017
1010
- $ url = $ this ->_getEndpoint ($ method , $ method_template );
1018
+ $ url = $ this ->_getEndpoint ($ method , $ method_template );
1019
+ $ authorization = null ;
1011
1020
$ ch = false ;
1012
- if ($ httpmethod == 'GET ' ) {
1021
+ $ request_headers = array ();
1022
+ if ($ httpmethod === 'GET ' ) {
1013
1023
$ url_with_params = $ url ;
1014
- if (count ($ params ) > 0 ) {
1024
+ if (json_encode ($ params ) !== ' {} ' ) {
1015
1025
$ url_with_params .= '? ' . http_build_query ($ params );
1016
1026
}
1017
1027
$ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1018
1028
$ ch = curl_init ($ url_with_params );
1019
1029
} else {
1020
1030
if ($ multipart ) {
1021
1031
$ authorization = $ this ->_sign ($ httpmethod , $ url , array ());
1022
- $ params = $ this ->_buildMultipart ($ method_template , $ params );
1032
+ $ params = $ this ->_buildMultipart ($ method , $ params );
1023
1033
} else {
1024
1034
$ authorization = $ this ->_sign ($ httpmethod , $ url , $ params );
1025
1035
$ params = http_build_query ($ params );
1026
1036
}
1027
1037
$ ch = curl_init ($ url );
1038
+ if ($ multipart ) {
1039
+ $ first_newline = strpos ($ params , "\r\n" );
1040
+ $ multipart_boundary = substr ($ params , 2 , $ first_newline - 2 );
1041
+ $ request_headers [] = 'Content-Type: multipart/form-data; boundary= '
1042
+ . $ multipart_boundary ;
1043
+ }
1028
1044
curl_setopt ($ ch , CURLOPT_POST , 1 );
1029
1045
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ params );
1030
1046
}
1031
1047
if ($ app_only_auth ) {
1032
- if (self ::$ _oauth_consumer_key == null ) {
1048
+ if (self ::$ _oauth_consumer_key === null ) {
1033
1049
throw new \Exception ('To make an app-only auth API request, the consumer key must be set. ' );
1034
1050
}
1035
1051
// automatically fetch bearer token, if necessary
1036
- if (self ::$ _oauth_bearer_token == null ) {
1052
+ if (self ::$ _oauth_bearer_token === null ) {
1037
1053
$ this ->oauth2_token ();
1038
1054
}
1039
- $ authorization = 'Authorization: Bearer ' . self ::$ _oauth_bearer_token ;
1055
+ $ authorization = 'Bearer ' . self ::$ _oauth_bearer_token ;
1040
1056
}
1041
- $ request_headers = array ();
1042
- if (isset ($ authorization )) {
1043
- $ request_headers [] = $ authorization ;
1057
+ if ($ authorization !== null ) {
1058
+ $ request_headers [] = 'Authorization: ' . $ authorization ;
1044
1059
$ request_headers [] = 'Expect: ' ;
1045
1060
}
1046
- if ($ multipart ) {
1047
- $ first_newline = strpos ($ params , "\r\n" );
1048
- $ multipart_boundary = substr ($ params , 2 , $ first_newline - 2 );
1049
- $ request_headers [] = 'Content-Length: ' . strlen ($ params );
1050
- $ request_headers [] = 'Content-Type: multipart/form-data; boundary= '
1051
- . $ multipart_boundary ;
1052
- }
1053
1061
1054
1062
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
1055
1063
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 0 );
0 commit comments