0

我在尝试将 XML API 数据检索到 Wordpress 页面时遇到了一些麻烦。我已经激活了自定义字段以连接到属性页面,获取 field_key 值并将它们的值分配给 XML API 标记。[在此处输入图像描述][1] 我在 Appereances 中的 functions.php 中设置了 Api 的基本身份验证>主题编辑器(如下图)但仍然无法从API获取任何数据!!!

function register_property_cpt(){
    register_post_type('property', [
        'label' => 'Properties',
        'public' => true,
        'capability_type' => 'post'
    ]);
}

add_action('wp_ajax_nopriv_get_properties_from_api', 'get_properties_from_api');
add_action('wp_ajax_get_properties_from_api', 'get_properties_from_api');

function get_properties_from_api(){

    $file = get_stylesheet_directory().'/report.txt';
    $current_page=(!empty($_POST['current_page'])) ? $_POST['current_page'] : 1;
    $properties = [];

    $url = 'https://api.justimmo.at/rest/v1/objekt/list';
    $args = array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( '(USER)' . ':' . '(PW)' )
        )
    );
    

    $results = wp_retrieve_body(wp_remote_get( $url, $args ));

    file_put_contents($file, "Current Page: ".$current_page."\n\n", FILE_APPEND);
    
    $xmlfile = file_get_contents($results);
    $new = simplexml_load_string($xmlfile);
    $connn = json_encode($new);
    $newArr = json_decode($connn, true);

    if( ! is_array($newArr) || empty($newArr)){
        return false;
    }

    $properties[] = $newArr;

    foreach ($properties[0] as $property){

        $property_slug = sanitize_title($property->name .'-'.$property->id);

        $inserted_property = wp_insert_post([
            'post_name' => $property_slug,
            'post_title' => $property_slug,
            'post_type' => 'property',
            'post_status' => 'publish'
        ]);

        if(is_wp_error($inserted_property)){
            continue;
        }

        $fillable = [
            'field_61249b1b2c02e' => 'objektnummer',
            'field_61249b2e2c02f' => 'objektkategorie',
            'field_61249b3d2c030' => 'titel',
            'field_61249baa2c031' => 'dreizeiler',
            'field_61249bb52c032' => 'naehe',
            'field_61249bbe2c033' => 'objektbeschreibung',
            'field_61249bd52c034' => 'etage',
            'field_61249bdc2c035' => 'strasse',
            'field_61249be22c036' => 'ort',
            'field_61249be82c037' => 'plz',
            'field_61249bed2c038' => 'preis',
            'field_61249bf22c039' => 'kaufpreis',
            'field_61249bfa2c03a' => 'wohnflaeche',
            'field_61249c012c03b' => 'groundflaeche',
            'field_61249c192c03c' => 'bueroflaeche',
            'field_61249c2d2c03d' => 'erstes_bild',
            'field_61249c372c03e' => 'status',
            'field_61249c3c2c03f' => 'status_id',
            'field_61249c412c040' => 'erstellt_am',
            'field_61249c4f2cf8d' => 'aktualisiert_am'
        ];

        foreach ($fillable as $key => $name){
            update_field($key, $property->$name, $inserted_property);
        }
    }

    $current_page = $current_page + 1;
    wp_remote_post( admin_url('admin-ajax.php?action=get_properties_from_api'), [
        'blocking' => false,
        'sslverify' => false,
        'body' => [
            'current_page' => $current_page
        ]
    ] );
}```


  [1]: https://i.stack.imgur.com/Yp5Go.png

As seen on a youtube video, the part 'admin-ajax.php?action=get_properties_from_api' should be added after the site.com/wp-admin for the API to run, but when I do so, it throws an error: "There has been a critical error on this website"

Any help would be appreciated, Thanks in advance.
4

0 回答 0