需要从PHP翻译成Java吗
尝试使用qwintryLogisticsAPI,所有的文档都是PHP的例子,我对PHP一无所知,在Java上编码。在需要的文档中,请求如下所示:需要从PHP翻译成Java吗,java,php,apache,httpclient,apache-httpclient-4.x,Java,Php,Apache,Httpclient,Apache Httpclient 4.x,尝试使用qwintryLogisticsAPI,所有的文档都是PHP的例子,我对PHP一无所知,在Java上编码。在需要的文档中,请求如下所示: <?php define('SITE_URL', 'logistics.qwintry.com'); define('API_KEY', 'YOUR_API_KEY'); //don't forget to set your key! $url = 'http://'. SITE_URL .'/api/cost';
<?php
define('SITE_URL', 'logistics.qwintry.com');
define('API_KEY', 'YOUR_API_KEY'); //don't forget to set your key!
$url = 'http://'. SITE_URL .'/api/cost';
$data = array (
'params' => array(
'weight' => 5, // in lb
'delivery_pickup' => 'msk_1', // full list of pickup points can be retrieved from /api/locations-list
'insurance' => true,
'items_value' => 500, // declaration items total cost in USD
'retail_pricing' => true // retail / wholesale pricing?
),
);
$data_string = http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. API_KEY));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
但不断得到回应:
{"success":false,"errorCode":0,"errorMessage":"params are empty"}
我认为问题在于为请求设置参数。请帮助我用Java术语翻译请求参数的结构 PHP代码数据参数不是路径参数,而是请求参数。您必须将它们写入请求主体,而不是将它们附加到URL中 您可以在相关问题中看到如何执行此操作:
不过,我会考虑更高级别的REST服务客户端,比如or。谢谢,开始使用Unirest库,看起来很简单,但仍然没有成功…请求主体的结构对我来说不清楚,因为我根本不懂PHP。如何使用字符串作为计数器生成参数数组?(参数[重量]、参数[物品价值]、…等等)终于成功了!API文档中有错误,一些参数的名称错误。现在一切都好了!
{"success":false,"errorCode":0,"errorMessage":"params are empty"}