2

I'm trying to get a list of locations from a website called weedmaps. This is the code that I'm using:

function call_for_api() {


    $url='https://api-v2.weedmaps.com/api/v2/listings';
    $response = wp_remote_post( $url, array(
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array('accept'=>'application/json','accept-encoding' => 'gzip, deflate, br','connection' =>'keep-alive'),
        'body' => null,
        'cookies' => array(),
        'compress'    => false,
            'decompress'  => true,
            'sslverify'   => true,
            'stream'      => false,
            'filename'    => null,  
        'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'
                )
        );

    if ( is_wp_error( $response ) ) {
         $error_message = $response->get_error_message();
        echo "Something went wrong: $error_message";
    } else {
         echo 'Response:<pre>';
         print_r( $response );
             echo '</pre>'; 
 echo wp_remote_retrieve_body( $response );

    }
}

function cf_shortcode() {
    ob_start();
    call_for_api();

    return ob_get_clean();
}

add_shortcode( 'weed-list', 'cf_shortcode' );

When I'm viewing the maps page on weedmaps . com and view my network tab this appears to be the right endpoint. I think I've missed something in how the wp_remote_post works.

I've never seen a 404 on api, but I'm a bit new to that.

It looks like I did the call right, but maybe the api experts see what I did wrong here.

This is the error message I get:

Array
(
    [headers] => Requests_Utility_CaseInsensitiveDictionary Object
        (
            [data:protected] => Array
                (
                    [access-control-allow-credentials] => true
                    [access-control-allow-methods] => GET, POST, PUT, DELETE, OPTIONS
                    [access-control-allow-origin] => https://weedmaps.com
                    [access-control-expose-headers] => 
                    [access-control-max-age] => 1728000
                    [content-type] => application/json; charset=UTF-8
                    [date] => Thu, 27 Jul 2017 04:37:10 GMT
                    [server] => nginx/1.4.6 (Ubuntu)
                    [vary] => Origin
                    [x-request-id] => 80e789df-9a6b-47af-9358-5b54626551e9
                    [x-runtime] => 0.008710
                    [content-length] => 34
                )

        )

    [body] => {"status":404,"error":"Not Found"}
    [response] => Array
        (
            [code] => 404
            [message] => Not Found
        )

1 Answer 1

3

Try using a GET request instead of a POST request. The headers indicate they only allow origin from weedmaps.com in the POST request. The method in WordPress is wp_remote_get().

    function call_for_api() {
        $url='https://api-v2.weedmaps.com/api/v2/listings';
        $response = wp_remote_get( $url,
            array(
                'timeout'     => 45,
                'redirection' => 5,
                'httpversion' => '1.0',
                'blocking'    => true,
                'headers'     => array(
                    'accept'          => 'application/json',
                    'accept-encoding' => 'gzip, deflate, br',
                    'connection'      =>'keep-alive'
                ),
                'body' => null,
                'cookies'     => array(),
                'compress'    => false,
                'decompress'  => true,
                'sslverify'   => true,
                'stream'      => false,
                'filename'    => null,  
                'user-agent'  => 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
            )
        );

        if ( is_wp_error( $response ) ) {
            $error_message = $response->get_error_message();
            echo "Something went wrong: $error_message";
        } else {
            echo 'Response:<pre>';
            print_r( $response );
            echo '</pre>'; 
            echo wp_remote_retrieve_body( $response );
        }
}

function cf_shortcode() {
    ob_start();
    call_for_api();

    return ob_get_clean();
}

add_shortcode( 'weed-list', 'cf_shortcode' );
1
  • man, i knew i missed something. forgot to change to "get"
    – rudtek
    Jul 27, 2017 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.