最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - REST API can't get the response manually

programmeradmin1浏览0评论

I am new at this. I don't know what is the problem here i want to get data from External REST Api endpoint but getting NO URL was found meanwhile when i use same URL with postman i get the response with no problem.

and this is my code to get the data manually.

$stidStore_name = 'Foulad Online';
$stidCustomer_name = 'Emraan Khan';
$stidPaymen_type = 'cod';
$stidAmount = 10;
$stidCustomer_country = 'BH';

$requestUrl = 'http://204.48.23.96:9002/testapi?store_name='.$stidStore_name.'&customer_name='.$stidCustomer_name.'&payment_type='.$stidPaymen_type.'&amount='.$stidAmount.'&customer_address='.$stidCustomer_country;
$request = new WP_REST_Request( 'POST', $requestUrl );

I am new at this. I don't know what is the problem here i want to get data from External REST Api endpoint but getting NO URL was found meanwhile when i use same URL with postman i get the response with no problem.

and this is my code to get the data manually.

$stidStore_name = 'Foulad Online';
$stidCustomer_name = 'Emraan Khan';
$stidPaymen_type = 'cod';
$stidAmount = 10;
$stidCustomer_country = 'BH';

$requestUrl = 'http://204.48.23.96:9002/testapi?store_name='.$stidStore_name.'&customer_name='.$stidCustomer_name.'&payment_type='.$stidPaymen_type.'&amount='.$stidAmount.'&customer_address='.$stidCustomer_country;
$request = new WP_REST_Request( 'POST', $requestUrl );
Share Improve this question edited Jun 19, 2020 at 8:33 Hammad Ahmed asked Jun 19, 2020 at 8:23 Hammad AhmedHammad Ahmed 11 bronze badge 2
  • How are you actually making the request? newing a WP_REST_Request won't be enough, and in any case I think that represents an incoming request, not an outgoing request. You probably want wp_remote_post(). – Rup Commented Jun 19, 2020 at 8:40
  • (It also seems odd you're passing all your parameters in the URL when you're POSTing the request, but I don't know your API.) – Rup Commented Jun 19, 2020 at 8:41
Add a comment  | 

1 Answer 1

Reset to default 1

I was using wrong method wp_remote_post() will work.

Here is the fix code:

$args = array(
'method'      => 'POST',
'timeout'     => 45,
'redirection' => 5,
'httpversion' => '1.0',
'body'     => array(
    'store_name' => $stidStore_name,
    'customer_name' =>$stidCustomer_name,
    'payment_type'=> $stidPaymen_type,
    'amount'=> $stidAmount,
    'customer_address'=> $stidCustomer_country,
),
);
$request = wp_remote_post( $stidUrl, $args);

$response = wp_remote_retrieve_body( $request );
$response = json_decode($response);
发布评论

评论列表(0)

  1. 暂无评论