【发布时间】:2014-12-24 17:25:48
【问题描述】:
我尝试使用 wp_remote_post() 缩短谷歌网址
但我得到了错误结果,
我知道如何使用 CURL,但 WordPress 不允许使用 CURL!
此 WordPress API 资源:
http://codex.wordpress.org/Function_Reference/wp_remote_post
http://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
http://codex.wordpress.org/HTTP_API#Other_Arguments
http://codex.wordpress.org/Function_Reference/wp_remote_post#Related
这个谷歌网址缩短 API 文档:
https://developers.google.com/url-shortener/v1/getting_started#shorten
这是我的代码:
function google_url_shrt{
$url = 'http://example-long-url.com/example-long-url'; // long url to short it
$args = array(
"headers" => array( "Content-type:application/json" ),
"body" => array( "longUrl" => $url )
);
$short = wp_remote_post("https://www.googleapis.com/urlshortener/v1/url", $args);
$retrieve = wp_remote_retrieve_body( $short );
$response = json_decode($retrieve, true);
echo '<pre>';
print_r($response);
echo '</pre>';
}
【问题讨论】:
标签: php json wordpress api curl