REST API无法手动获取响应

时间:2020-06-19 作者:Hammad Ahmed

这方面我是新手。我不知道这里有什么问题,我想从外部REST Api端点获取数据,但没有找到URL,同时,当我与postman使用相同的URL时,我得到的响应没有问题。

enter image description here

这是我手动获取数据的代码。

$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 );

1 个回复
SO网友:Hammad Ahmed

我使用了错误的方法wp\\u remote\\u post()将起作用。

以下是修复代码:

$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);