skip to Main Content

im trying to get the xml but i always get a 403 error. But with postman its working fine.

i dont think i did something wrong with the authorization but maybe im wrong.

this code is in the functions.php and im calling it with https://localhost/wordpress/wp-admin/admin-ajax.php?action=nopriv_search_mobileads

add_action('wp_ajax_nopriv_search_mobileads', 'get_mobileads_from_api');
add_action('wp_ajax_search_mobileads', 'get_mobileads_from_api');
function get_mobileads_from_api() {
    $url = ('https://services.mobile.de/search-api/search?');
    
    $wp_request_headers = array(
        'Host'  => 'https://services.mobile.de',
        'Accept' => 'application/xml',
        'Authorization' => 'Basic '.base64_encode('id:pw')
    );


    $results = wp_remote_request(
        $url,
        array(
            'method'    => 'GET',
            'headers'   => $wp_request_headers
        )
    );
   print_r($results);
}

output:

Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary Object ( [data:protected] => Array ( ) ) [body] => [response] => Array ( [code] => 403 [message] => Forbidden ) [cookies] => Array ( ) [filename] => [http_response] => WP_HTTP_Requests_Response Object ( [response:protected] => Requests_Response Object ( [body] => [raw] => HTTP/1.1 403 [headers] => Requests_Response_Headers Object ( [data:protected] => Array ( ) ) [status_code] => 403 [protocol_version] => 1.1 [success] => [redirects] => 0 [url] => https://services.mobile.de/search-api/search? [history] => Array ( ) [cookies] => Requests_Cookie_Jar Object ( [cookies:protected] => Array ( ) ) ) [filename:protected] => [data] => [headers] => [status] => ) ) 0

2

Answers


  1. Chosen as BEST ANSWER

    found a way.

    add_action('wp_ajax_nopriv_get_ads_from_api','get_ads_from_api');
    add_action('wp_ajax_get_ads_from_api','get_ads_from_api');
    function get_ads_from_api(){
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://services.mobile.de/search-api/search?',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'GET',
          CURLOPT_HTTPHEADER => array(
            'Authorization: Basic ***'
          ),
        ));
    
        $response = curl_exec($curl);
    
        curl_close($curl);
        echo $response;
    }
    

  2. Unless the actual username is "id" and the actual password is "pw" that’s not going to work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search