19 /** |
22 /** |
20 * Constructor. |
23 * Constructor. |
21 * |
24 * |
22 * @since 2.8.0 |
25 * @since 2.8.0 |
23 * @since 3.2.0 Updated to use a PHP5 constructor. |
26 * @since 3.2.0 Updated to use a PHP5 constructor. |
|
27 * @since 5.6.1 Multiple headers are concatenated into a comma-separated string, |
|
28 * rather than remaining an array. |
24 * |
29 * |
25 * @param string $url Remote file URL. |
30 * @param string $url Remote file URL. |
26 * @param integer $timeout Optional. How long the connection should stay open in seconds. |
31 * @param int $timeout Optional. How long the connection should stay open in seconds. |
27 * Default 10. |
32 * Default 10. |
28 * @param integer $redirects Optional. The number of allowed redirects. Default 5. |
33 * @param int $redirects Optional. The number of allowed redirects. Default 5. |
29 * @param string|array $headers Optional. Array or string of headers to send with the request. |
34 * @param string|array $headers Optional. Array or string of headers to send with the request. |
30 * Default null. |
35 * Default null. |
31 * @param string $useragent Optional. User-agent value sent. Default null. |
36 * @param string $useragent Optional. User-agent value sent. Default null. |
32 * @param boolean $force_fsockopen Optional. Whether to force opening internet or unix domain socket |
37 * @param bool $force_fsockopen Optional. Whether to force opening internet or unix domain socket |
33 * connection or not. Default false. |
38 * connection or not. Default false. |
34 */ |
39 */ |
35 public function __construct( $url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false ) { |
40 public function __construct( $url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false ) { |
36 $this->url = $url; |
41 $this->url = $url; |
37 $this->timeout = $timeout; |
42 $this->timeout = $timeout; |
58 $res = wp_safe_remote_request( $url, $args ); |
63 $res = wp_safe_remote_request( $url, $args ); |
59 |
64 |
60 if ( is_wp_error( $res ) ) { |
65 if ( is_wp_error( $res ) ) { |
61 $this->error = 'WP HTTP Error: ' . $res->get_error_message(); |
66 $this->error = 'WP HTTP Error: ' . $res->get_error_message(); |
62 $this->success = false; |
67 $this->success = false; |
|
68 |
63 } else { |
69 } else { |
64 $this->headers = wp_remote_retrieve_headers( $res ); |
70 $this->headers = wp_remote_retrieve_headers( $res ); |
|
71 |
|
72 /* |
|
73 * SimplePie expects multiple headers to be stored as a comma-separated string, |
|
74 * but `wp_remote_retrieve_headers()` returns them as an array, so they need |
|
75 * to be converted. |
|
76 * |
|
77 * The only exception to that is the `content-type` header, which should ignore |
|
78 * any previous values and only use the last one. |
|
79 * |
|
80 * @see SimplePie_HTTP_Parser::new_line(). |
|
81 */ |
|
82 foreach ( $this->headers as $name => $value ) { |
|
83 if ( ! is_array( $value ) ) { |
|
84 continue; |
|
85 } |
|
86 |
|
87 if ( 'content-type' === $name ) { |
|
88 $this->headers[ $name ] = array_pop( $value ); |
|
89 } else { |
|
90 $this->headers[ $name ] = implode( ', ', $value ); |
|
91 } |
|
92 } |
|
93 |
65 $this->body = wp_remote_retrieve_body( $res ); |
94 $this->body = wp_remote_retrieve_body( $res ); |
66 $this->status_code = wp_remote_retrieve_response_code( $res ); |
95 $this->status_code = wp_remote_retrieve_response_code( $res ); |
67 } |
96 } |
68 } else { |
97 } else { |
69 $this->error = ''; |
98 $this->error = ''; |