猿问

Wordpress - 在db中记录,从json获取的数据

我编写了这个插件,它可以完美地接收和显示来自外部 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 的数据将它们注册到数据库中吗?


GCT1015
浏览 135回答 1
1回答

哆啦的时光机

可以帮助你:global $wpdb;$table = $wpdb->prefix.'you_table_name';$data = array('column1' => 'data one', 'column2' => 123);$format = array('%s','%d');$wpdb->insert($table,$data,$format);$my_id = $wpdb->insert_id;
随时随地看视频慕课网APP
我要回答