简体   繁体   English

从Wordpress中的wp_remote_get访问JSON

[英]Access JSON from wp_remote_get in Wordpress

I'm trying to access the json that's returned from this Google books API request using wp_remote_get but its not outputting the data. 我正在尝试使用wp_remote_get访问从此Google图书API请求返回的json,但它不输出数据。 Can anyone tell me what the problem is? 谁能告诉我这是什么问题?

$request = wp_remote_get('https://books.google.com/books?bibkeys=9780001955073%2C%209780001982116%2C%209780001981768&jscmd=viewapi&callback=listisbns');

$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );

foreach( $data as $book ) {
   echo $book->info_url;
}

It would be ok if you were using JavaScript via the callback function listisbns , to use the returned result in PHP you gotta clean up the returned string: 如果您通过回调函数listisbns使用JavaScript,在PHP中使用返回的结果,则必须清理返回的字符串:

$body = wp_remote_retrieve_body( $request );

# clean start removing "listisbns("
$body = str_replace( 'listisbns(', '', $body );

# clean end removing last two characters: ");"
$body = substr( $body, 0, strlen($body) - 2 );

# data ok to proceed
$data = json_decode( $body );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM