[英]How to fix wp_remote_get() error // get value from an array?
I want to get each Bank_name
on WordPress from the array while calling an external Api. 我想在调用外部Api的同时从阵列中获取WordPress上的每个Bank_name
。 I used wp_remote_get()
. 我使用了wp_remote_get()
。 While doing the foreach
loop with the array, I got this error: 在使用数组执行foreach
循环时,我收到此错误:
Invalid argument supplied for foreach(). 为foreach()提供的参数无效。
To debug the error, I tried echoing first one Bank_name
by providing an index. 为了调试错误,我尝试通过提供索引Bank_name
第一个Bank_name
。 I did exact same thing in two ways but one works and the other one doesn't. 我用两种方式做了同样的事情,但一种是有效的,另一种则没有。 The only difference is that, one way uses wp_remote_get()
, while other one uses the response as an array without calling any api. 唯一的区别是,一种方法使用wp_remote_get()
,而另一种方法使用响应作为数组而不调用任何api。
Where is the error coming from? 错误来自哪里?
This doesn't work: 这不起作用:
$request = wp_remote_get( 'https://tv-api-dev.azurewebsites.net/api/GetBankInfo' );
if( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request );
$test = json_decode( $body, true );
print_r($test) ;
echo $test[0]["Bank_name"];
The following works fine: 以下工作正常:
$someJSON = "[\r\n {\r\n \"Bank_id\": 1,\r\n \"Bank_name\": \"Aktia Pankki\"\r\n },\r\n {\r\n \"Bank_id\": 2,\r\n \"Bank_name\": \"Bigbank\"\r\n },\r\n {\r\n \"Bank_id\": 3,\r\n \"Bank_name\": \"POP Pankit\"\r\n },\r\n {\r\n \"Bank_id\": 4,\r\n \"Bank_name\": \"Bonum Pankki\"\r\n },\r\n {\r\n \"Bank_id\": 5,\r\n \"Bank_name\": \"Citibank\"\r\n },\r\n {\r\n \"Bank_id\": 6,\r\n \"Bank_name\": \"Danske Bank\"\r\n },\r\n {\r\n \"Bank_id\": 8,\r\n \"Bank_name\": \"DNB BANK ASA\"\r\n },\r\n {\r\n \"Bank_id\": 9,\r\n \"Bank_name\": \"Handelsbanken\"\r\n },\r\n {\r\n \"Bank_id\": 10,\r\n \"Bank_name\": \"Holvi\"\r\n },\r\n {\r\n \"Bank_id\": 11,\r\n \"Bank_name\": \"Nordea Pankki\"\r\n },\r\n {\r\n \"Bank_id\": 12,\r\n \"Bank_name\": \"OP Ryhmä\"\r\n },\r\n {\r\n \"Bank_id\": 13,\r\n \"Bank_name\": \"S-Pankki\"\r\n },\r\n {\r\n \"Bank_id\": 14,\r\n \"Bank_name\": \"Swedbank\"\r\n },\r\n {\r\n \"Bank_id\": 15,\r\n \"Bank_name\": \"Säästöpankki\"\r\n },\r\n {\r\n \"Bank_id\": 16,\r\n \"Bank_name\": \"Alandsbanken\"\r\n }\r\n]";
// Convert JSON string to Array
$someArray = json_decode($someJSON, true);
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["Bank_name"]; // Access Array data
echo $someArray[0]["Bank_id"]; // Access Array data
From 2nd way, I get output Aktia Pankki1, which is what I expect also from the 1st way but I get this message: 从第二种方式,我得到输出Aktia Pankki1,这是我期望从第一种方式,但我得到这个消息:
Warning: Illegal string offset 'Bank_name' in C:\\xampp\\htdocs\\wpsummerproject\\wp-content\\themes\\astra\\functions.php on line 181 警告:第181行的C:\\ xampp \\ htdocs \\ wpsummerproject \\ wp-content \\ themes \\ astra \\ functions.php中的非法字符串偏移'Bank_name'
I tried the data from the given URL. 我尝试了来自给定URL的数据。 Not sure why but in the first json_decode()
, the obtained value is the single JSON object with sub items. 不知道为什么但是在第一个json_decode()
,获得的值是带有子项的单个JSON对象。 When that value is again called with json_decode()
then value is obtained as an array. 当使用json_decode()
再次调用该值时,将获得值作为数组。 You may want to check in the source first how data is converted to JSON. 您可能希望首先检查数据源如何将数据转换为JSON。 There seems to be the problem. 似乎有问题。
$request = wp_remote_get( 'https://tv-api-dev.azurewebsites.net/api/GetBankInfo' );
if( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request );
$body_content = json_decode( $body, true );
$details = json_decode( $body_content, true );
print_r( $details[0]['Bank_name']);
print_r( $details[0]['Bank_id']);
Try: 尝试:
$url = 'https://tv-api-dev.azurewebsites.net/api/GetBankInfo';
$response = wp_remote_get( esc_url_raw( $url ) );
/* Will result in $api_response being an array of data,
parsed from the JSON response of the API listed above */
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
This is an example from codex.wordpress.org Your link: https://tv-api-dev.azurewebsites.net/api/GetBankInfo goes to a valid json. 这是来自codex.wordpress.org的示例您的链接: https ://tv-api-dev.azurewebsites.net/api/GetBankInfo转到有效的json。 Should work. 应该管用。
This will work in your case, 这适用于您的情况,
$request = wp_remote_get( 'https://tv-api-dev.azurewebsites.net/api/GetBankInfo' );
if( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request );
$test = json_decode( $body,true );
$test = json_decode($test,true);
echo $test[0]['Bank_name'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.