使用WordPress函数wp_remote_get向远程api发出Get和Post请求
WordPress内部封装了不少看上去和博客无关的功能,很多都是对php已有的功能的一些个性化和优化等。如果是用WordPress平台构建网站,可以使用这些函数来替代php原生函数。
下面实例是原创抓取一下百度的首页。
$args = array(
'timeout' => 10,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'stream' => false,
'filename' => null
);
$response = wp_remote_get( 'https://www.baidu.com' ,$args);
if ( is_array( $response ) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
}echo $body