1: <?php
2:
3: 4: 5:
6: class RESTian_WordPress_Http_Agent extends RESTian_Http_Agent_Base {
7: 8: 9: 10: 11: 12: 13:
14: function make_request( $request, $response ) {
15:
16: switch ( $request->http_method ) {
17: case 'GET':
18: $url = $request->get_url();
19: $args = $this->get_args( $request );
20: $result = wp_remote_get( $url, $args );
21: break;
22: case 'POST':
23: if ( $content_type = $request->get_content_type() )
24: $request->add_header( 'Content-type', $content_type );
25: $url = $request->get_url();
26: $args = $this->get_args( $request );
27: $result = wp_remote_post( $url, $args );
28: break;
29: case 'PUT':
30: $result = new WP_Error( -1, 'HTTP PUT not yet supported.' );
31: break;
32: case 'DELETE':
33: $result = new WP_Error( -2, 'HTTP DELETE not yet supported.' );
34: break;
35: }
36:
37: if ( method_exists( $request->client, 'filter_result' ) )
38: $result = $request->client->filter_result( $result, $response );
39:
40: if ( is_wp_error( $result ) ) {
41: 42: 43:
44: $error_num = $result->get_error_code();
45: $response->set_http_error( $error_num, $result->get_error_message( $error_num ) );
46: } else {
47: $response->message = wp_remote_retrieve_response_message( $result );
48: }
49:
50: $response->status_code = wp_remote_retrieve_response_code( $result );
51: $response->body = wp_remote_retrieve_body( $result );
52: $response->body = $request->client->apply_filters( 'result_body', $response->body, $response );
53:
54: $response->result = $result;
55:
56: return $response;
57: }
58:
59: 60: 61: 62: 63:
64: function get_args( $request ) {
65: $args = array(
66: 'method' => $request->http_method,
67: 'headers' => $request->get_headers(),
68: 'body' => $request->get_body(),
69: 'sslverify' => $request->sslverify,
70: 'user-agent' => $request->client->get_user_agent(),
71: );
72: return $args;
73: }
74:
75:
76: }
77:
78: