我编写了这个插件,它可以完美地接收和显示来自外部 API 的数据。
代码:
function get_posts_via_rest() {
$request = wp_remote_get( 'http://xxx' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$risposta_json = json_decode( $body );
//print_r($data);
if( ! empty( $risposta_json ) ) {
foreach($risposta_json->data->offerte->data as $offerta ){ // now iterate through that array
if($offerta->struttura->id == aggancio_id_campo_uno()){ // check all conditions
echo '<ul>';
echo '<li>'.$offerta->struttura->nome .'</li>';
echo '<li>'.$offerta->struttura->url_completo .'</li>';
echo '<li>'.$offerta->titolo .'</li>';
echo '</ul>';
}
}
}
}
// Register as a shortcode to be used on the site.
add_shortcode( 'sc_get_posts_via_rest', 'get_posts_via_rest' );
现在我创建了一种名为出价的自定义帖子,因为此代码显示了给定公司 ID 的所有出价,该 ID 设置了一个输入字段,我没有把它放在这里,因为它是多余的,我的问题是我该怎么做现在有来自 json 的数据将它们注册到数据库中吗?
哆啦的时光机