在php中从JSON打印特定数据
您好,我对wordpress环境中的json和cURL相当陌生。我试图清理从cURL响应输出的json数据。现在就这样开始在php中从JSON打印特定数据,php,json,wordpress,curl,Php,Json,Wordpress,Curl,您好,我对wordpress环境中的json和cURL相当陌生。我试图清理从cURL响应输出的json数据。现在就这样开始 {"resultCode":"SUCCESS", "resultReason":null, "resultDetail":null, "data":{"page_count":50, "total_count":1948, "product_list":[{"product_id":"014a20b1-dbe7-11e8-a53e-0236c
{"resultCode":"SUCCESS",
"resultReason":null,
"resultDetail":null,
"data":{"page_count":50,
"total_count":1948,
"product_list":[{"product_id":"014a20b1-dbe7-11e8-a53e-0236cac9056a",
"product_status":"ACTIVE",
"last_updated_at":"2019-10-07T15:43:54.000-...
我希望在“数据”之后打印时从这里开始:
我使用的php函数如下所示:
function cornerstonetreez () {
$ch = curl_init();
curl_setopt($ch,
CURLOPT_URL,"https://api.treez.io/v2.0/dispensary/cornerstone/product/product_list?
category_type=all&page=1&pagesize=50&active=true");
$headers = array();
$headers[] = 'authorization: authorizationkeyhere';
$headers[] = 'client_id: clientkey';
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
$json_string = json_encode($server_output, JSON_PRETTY_PRINT);
print $json_string ;
}
add_shortcode( 'helloworld', 'cornerstonetreez' );
任何帮助都将不胜感激。谢谢大家! 这有用吗
<?php function cornerstonetreez () {
$ch = curl_init();
curl_setopt($ch,
CURLOPT_URL,"https://api.treez.io/v2.0/dispensary/cornerstone/product/product_list?
category_type=all&page=1&pagesize=50&active=true");
$headers = array();
$headers[] = 'authorization: authorizationkeyhere';
$headers[] = 'client_id: clientkey';
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$server_output = json_decode(curl_exec ($ch));
curl_close ($ch);
$json_string = json_encode($server_output->data, JSON_PRETTY_PRINT);
print $json_string ;
}
add_shortcode( 'helloworld', 'cornerstonetreez' );
这行吗
<?php function cornerstonetreez () {
$ch = curl_init();
curl_setopt($ch,
CURLOPT_URL,"https://api.treez.io/v2.0/dispensary/cornerstone/product/product_list?
category_type=all&page=1&pagesize=50&active=true");
$headers = array();
$headers[] = 'authorization: authorizationkeyhere';
$headers[] = 'client_id: clientkey';
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$server_output = json_decode(curl_exec ($ch));
curl_close ($ch);
$json_string = json_encode($server_output->data, JSON_PRETTY_PRINT);
print $json_string ;
}
add_shortcode( 'helloworld', 'cornerstonetreez' );
首先需要将JSONString转换为PHP对象或数组。然后,您可以寻址您想要的部分,即$obj->data
然后,您可能需要将其转换为另一个JSON字符串,以便将其传递到打印到的任何位置
$server_output = curl_exec ($ch);
$obj = json_decode($server_output);
print json_encode($obj->data);
首先,需要将JSONString转换为PHP对象或数组。然后,您可以寻址您想要的部分,即$obj->data
然后,您可能需要将其转换为另一个JSON字符串,以便将其传递到打印到的任何位置
$server_output = curl_exec ($ch);
$obj = json_decode($server_output);
print json_encode($obj->data);
所以你只想得到“数据”?是的,我只想得到数据你只想得到“数据”?是的,我只想得到数据不,它不起作用。它仍然显示所有内容{“resultCode”:“SUCCESS”,“resultReason”:null,“resultDetail”:null,“data”:{“page\u count”:50yup仍然不起作用..我仍然得到完整的结果我想这是因为您没有将RETURNTRANSFER添加到curl,这导致curl_exec打印并在底部再次打印,请检查我的更新代码!不起作用。它仍然显示所有内容{“resultCode”:“SUCCESS”,“resultReason”:null,“resultDetail”:null,“data”:{“page_count”:50yup仍然不起作用..我仍然得到完整的结果我想这是因为您没有将RETURNTRANSFER添加到curl,这会导致curl_exec打印并在底部再次打印,请检查我的更新代码!这是我得到的结果:我在函数末尾使用了:$obj=json_decode($server_output);curl_close($ch);$obj=json_decode($server_output);$json_string=json_encode($obj->data);打印$json_string;这就是我得到的结果:我在函数的末尾使用了它:$obj=json_decode($server_output);curl_close($ch);$obj=json_decode($server_output);$json_string=json_encode($obj->数据);打印$json_string=json_encode($obj->数据);