|
15 | 15 | * @link http://be.net/dev |
16 | 16 | * |
17 | 17 | */ |
18 | | -class Api { |
| 18 | +class Client { |
19 | 19 |
|
20 | 20 | const API_ROOT = 'https://www.behance.net/v2'; |
21 | 21 | const ENDPOINT_PROJECTS = '/projects'; |
@@ -48,10 +48,6 @@ class Api { |
48 | 48 | */ |
49 | 49 | public function __construct( $client_id, $debug = false ) { |
50 | 50 |
|
51 | | - if ( !extension_loaded( 'curl' ) ) { |
52 | | - throw new ApiException( 'cURL module is required' ); |
53 | | - } |
54 | | - |
55 | 51 | $this->_client_id = $client_id; |
56 | 52 | $this->_debug = $debug; |
57 | 53 |
|
@@ -197,28 +193,6 @@ public function getUserFollowers( $id_or_username, $options = array(), $assoc = |
197 | 193 |
|
198 | 194 | } // getUserFollowers |
199 | 195 |
|
200 | | - /** |
201 | | - * Retrieves a list of users in the given user's feedback circle |
202 | | - * |
203 | | - * @param int|string $id_or_username : user |
204 | | - * @param bool $assoc : return objects will be converted to associative arrays |
205 | | - * @param array $options : search options |
206 | | - * |
207 | | - * @return array stdClass objects or associative arrays, based on $assoc |
208 | | - */ |
209 | | - public function getUserFeedbackCircle( $id_or_username, $options = array(), $assoc = false ) { |
210 | | - |
211 | | - $endpoint = self::ENDPOINT_USERS . '/' . $id_or_username . '/feedback'; |
212 | | - |
213 | | - $results = $this->_getDecodedJson( $endpoint, $options, 'feedback_circlezz', $assoc ); |
214 | | - |
215 | | - // IMPORTANT: Ensure this will always return an array |
216 | | - return ( empty( $results ) ) |
217 | | - ? array() |
218 | | - : $results; |
219 | | - |
220 | | - } // getUserFeedbackCircle |
221 | | - |
222 | 196 | /** |
223 | 197 | * Retrieves a list of $id_or_username's works in progress |
224 | 198 | * |
@@ -549,129 +523,69 @@ protected function _makeFullURL( $endpoint, array $query_params = array() ) { |
549 | 523 | */ |
550 | 524 | protected function _executeRequest( $method, $url, $request_body = false, $curl_params = array() ) { |
551 | 525 |
|
| 526 | + if ( !$this->_isCurlLoaded() ) { |
| 527 | + throw new ApiException( 'cURL module is required' ); |
| 528 | + } |
| 529 | + |
552 | 530 | $user_agent = "Behance API/PHP (App {$this->_client_id})"; |
553 | 531 | $default_curl_params = array( |
554 | 532 | CURLOPT_HTTPHEADER => array( |
555 | 533 | 'Accept: application/json', |
556 | 534 | 'Content-Type: multipart/form-data', |
557 | 535 | 'Expect:' |
558 | 536 | ), |
559 | | - CURLOPT_TIMEOUT => self::TIMEOUT_DEFAULT_SEC, |
560 | | - CURLOPT_USERAGENT => $user_agent, |
561 | | - CURLOPT_RETURNTRANSFER => true, |
562 | | - CURLOPT_BINARYTRANSFER => true, |
563 | | - CURLOPT_HEADER => true, |
564 | | - CURLOPT_SSL_VERIFYPEER => false |
| 537 | + CURLOPT_TIMEOUT => self::TIMEOUT_DEFAULT_SEC, |
| 538 | + CURLOPT_USERAGENT => $user_agent, |
| 539 | + CURLOPT_RETURNTRANSFER => true, |
| 540 | + CURLOPT_BINARYTRANSFER => true, |
| 541 | + CURLOPT_SSL_VERIFYPEER => false, |
| 542 | + CURLOPT_POST => false, |
| 543 | + CURLOPT_HTTPGET => true |
565 | 544 | ); |
566 | 545 |
|
567 | | - // Replace recursive will knock this *into* an array afterwards |
568 | | - if ( !empty( $curl_params[ CURLOPT_HTTPHEADER ] ) ) { |
569 | | - unset( $default_curl_params[ CURLOPT_HTTPHEADER ] ); |
570 | | - } |
571 | | - |
572 | 546 | $curl_params = array_replace_recursive( $default_curl_params, $curl_params ); |
573 | | - $method = strtoupper( $method ); |
574 | | - |
575 | | - switch ( $method ) { |
576 | | - |
577 | | - case 'GET': |
578 | | - |
579 | | - $curl_params[ CURLOPT_HTTPGET ] = true; |
580 | | - $curl_params[ CURLOPT_POST ] = false; |
581 | | - break; |
582 | | - |
583 | | - case 'POST': |
584 | | - |
585 | | - $curl_params[ CURLOPT_HTTPGET ] = false; |
586 | | - $curl_params[ CURLOPT_POST ] = true; |
587 | | - |
588 | | - // IMPORTANT: Since @ is used to signify files in arrays passed to this option, |
589 | | - // pre-encode this array to prevent this from attempting to read a file |
590 | | - // if ( is_array( $request_body ) ) |
591 | | - // $request_body = http_build_query( $request_body ); |
592 | | - |
593 | | - // $curl_params[ CURLOPT_HTTPHEADER ][] = 'Content-Length: ' . strlen( $request_body ); |
594 | | - $curl_params[ CURLOPT_POSTFIELDS ] = $request_body; |
595 | | - break; |
596 | | - |
597 | | - case 'PUT': |
598 | | - |
599 | | - $curl_params[ CURLOPT_HTTPGET ] = false; |
600 | | - $curl_params[ CURLOPT_POST ] = false; |
601 | | - $curl_params[ CURLOPT_CUSTOMREQUEST ] = 'PUT'; |
602 | | - |
603 | | - // IMPORTANT: Since @ is used to signify files in arrays passed to this option, |
604 | | - // pre-encode this array to prevent this from attempting to read a file |
605 | | - if ( is_array( $request_body ) ) { |
606 | | - $request_body = http_build_query( $request_body ); |
607 | | - } |
608 | | - |
609 | | - $curl_params[ CURLOPT_HTTPHEADER ][] = 'Content-Length: ' . strlen( $request_body ); |
610 | | - $curl_params[ CURLOPT_POSTFIELDS ] = $request_body; |
611 | | - break; |
612 | | - |
613 | | - case 'DELETE': |
614 | | - |
615 | | - $curl_params[ CURLOPT_HTTPGET ] = false; |
616 | | - $curl_params[ CURLOPT_CUSTOMREQUEST ] = 'DELETE'; |
617 | | - |
618 | | - // IMPORTANT: Since @ is used to signify files in arrays passed to this option, |
619 | | - // pre-encode this array to prevent this from attempting to read a file |
620 | | - if ( is_array( $request_body ) ) { |
621 | | - $request_body = http_build_query( $request_body ); |
622 | | - } |
623 | | - |
624 | | - $curl_params[ CURLOPT_POSTFIELDS ] = $request_body; |
625 | | - break; |
626 | | - |
627 | | - default: |
628 | | - throw new ApiException( "Unhandled method: [{$method}]" ); |
629 | | - |
630 | | - } // switch method |
631 | 547 |
|
632 | 548 | $ch = curl_init( $url ); |
633 | 549 |
|
634 | 550 | curl_setopt_array( $ch, $curl_params ); |
635 | 551 |
|
636 | | - $request_response = curl_exec( $ch ); |
637 | | - |
638 | | - $request_info = curl_getinfo( $ch ); |
639 | | - $response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); |
| 552 | + list( $response_body, $request_info, $response_code ) = $this->_executeCurl( $ch ); |
640 | 553 |
|
641 | 554 | curl_close( $ch ); |
642 | 555 |
|
643 | | - $response_body = false; |
644 | | - |
645 | | - if ( $request_info['download_content_length'] <= 0 ) { |
| 556 | + // @throws ApiException on response non-2xx (success) responses from service |
| 557 | + if ( (int)round( $response_code, -2 ) !== 200 ) { |
| 558 | + throw new ApiException( "Unsuccessful Request, response ({$response_code}): " . ( empty( $response_body ) ? '' : ": {$response_body} " ) ); |
| 559 | + } |
646 | 560 |
|
647 | | - $exploded = explode( "\r\n\r\n", $request_response ); |
| 561 | + return $response_body; |
648 | 562 |
|
649 | | - while ( $exploded[0] == 'HTTP/1.1 100 Continue' ) { |
650 | | - array_shift( $exploded ); |
651 | | - } |
| 563 | + } // _executeRequest |
652 | 564 |
|
653 | | - $response_body = ( isset( $exploded[1] ) ) |
654 | | - ? $exploded[1] |
655 | | - : ''; |
| 565 | + /** |
| 566 | + * @codeCoverageIgnore |
| 567 | + * |
| 568 | + * @return boolean |
| 569 | + */ |
| 570 | + protected function _isCurlLoaded() { |
656 | 571 |
|
657 | | - } // if download_content_length = 0 |
| 572 | + return extension_loaded( 'curl' ); |
658 | 573 |
|
659 | | - else { |
660 | | - $response_body = substr( $request_response, -$request_info['download_content_length'] ); |
661 | | - } // else |
| 574 | + } // _isCurlLoaded |
662 | 575 |
|
663 | | - // Unless array_shift completely solves headers in body problem, leave this line in |
664 | | - if ( substr( $response_body, 0, 4 ) == 'HTTP' ) { |
665 | | - throw new ApiException( "Malformed response_body: " . var_export( $response_body, 1 ) ); |
666 | | - } |
| 576 | + /** |
| 577 | + * @codeCoverageIgnore |
| 578 | + * |
| 579 | + * @return array |
| 580 | + */ |
| 581 | + protected function _executeCurl( $ch ) { |
667 | 582 |
|
668 | | - // @throws ApiException on response non-2xx (success) responses from service |
669 | | - if ( (int)round( $response_code, -2 ) !== 200 ) { |
670 | | - throw new ApiException( "Unsuccessful Request, response ({$response_code}): " . ( empty( $response_body ) ? '' : ": {$response_body} " ) ); |
671 | | - } |
| 583 | + $request_response = curl_exec( $ch ); |
| 584 | + $request_info = curl_getinfo( $ch ); |
| 585 | + $response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); |
672 | 586 |
|
673 | | - return $response_body; |
| 587 | + return array( $request_response, $request_info, $response_code ); |
674 | 588 |
|
675 | | - } // _executeRequest |
| 589 | + } // _executeCurl |
676 | 590 |
|
677 | | -} // Api |
| 591 | +} // Client |
0 commit comments