我想从一个叫做weedmaps
. 这是我正在使用的代码:
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\' );
当我在weedmaps上查看地图页面时。com和“查看我的网络”选项卡这似乎是正确的端点。我想我错过了一些东西
wp_remote_post
作品
我从未在api上见过404,但我对它有点陌生。
看起来我的调用是对的,但也许api专家看到了我做错了什么。
这是我收到的错误消息:
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
)
最合适的回答,由SO网友:Tim Elsass 整理而成
尝试使用GET请求而不是POST请求。标题表示它们只允许来源于weedmaps。POST请求中的com。WordPress中的方法是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\' );