【问题标题】:post data to api using json使用 json 将数据发布到 api
【发布时间】:2015-03-16 12:42:49
【问题描述】:

我编写的这段代码是为了使用 JSON 中的 post 方法将数据发送到 api,但作为响应,我变得无效,谁能告诉我代码中有什么问题。

$post = array(
    "operatorCode"=>"9",
    "scheduleCode"=>"84W92XC8LOBAF3KZP4",
    "travelDate"=>"2015-03-20",
    "fromStationCode"=>"84",
    "toStationCode"=>"76",
    "boardingPointCode"=>"191933",
    "droppingPointCode"=>"0000",
    "email"=>"*********",
    "mobile"=>"*******",
    "passenger"=>array(
                    "seatNumber"=>"40",
                    "name"=>"*****",
                    "age"=>"20",
                    "gender"=>"Male"
                 )
);

$data_string = json_encode($post);
echo $data_string;   
$ch=url_init('http://api?username=*****');   
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);
var_dump($result);
echo'<pre>';
print_r($result);

来自 api 的响应

stdClass Object
(
   [code] => 0
   [errorCode] => ER30
   [errorDesc] => Invalid Data
   [message] => Failure
)

示例网址:

http://<ServerName>/bookbustickets/rest/json/2.0/tempbooking?username=****&password‌​=****** 

示例输入数据:

 data={"operatorCode":"2586", 
       "scheduleCode":"7Q52586C3YG8KJ350F2F5V264", 
       "travelDa‌​te":"2015-03-10", 
       "fromStationCode":"76", 
       "toStationCode":"75", 
       "boardingPointCode":‌​"284936", 
       "droppingPointCode":"0000",
       "email":"test@gmail.com",
       "mobile":"9876543210‌​",
       "passenger":[
                     {
                        "seatNumber":"3",
                        "na 9 me":"Test",
                        "age":20,
                        "gender":"Male"},
                      {
                        "seatNumber":"4",
                        "name":"Test",
                        "age":20,
                        "g‌​ender":"Male"}
                    ]
        }

【问题讨论】:

  • 更具体的错误信息。是 curl 错误还是 API 返回的错误?
  • 响应即将到来,但显示无效数据和失败
  • 响应来自哪里????在您的问题中显示回复编辑您的问题不要将其放在评论中
  • 来自 api 的响应
  • 屏幕截图在这里。如果是 curl 错误,请尝试学习如何使用 curl_error php.net/curl_error 以及 curl_errno php.net/curl-errno

标签: php json api post curl


【解决方案1】:

好的,问题是"passenger" 应该是一个对象数组,所以尝试将代码更改为

$passenger1 = new stdClass();
$passenger1->seatNumber = "40";
$passenger1->name = "*****";
$passenger1->age = "20";
$passenger1->gender = "Male";

$passenger2 = new stdClass();
$passenger2->seatNumber = "41";
$passenger2->name = "*****";
$passenger2->age = "21";
$passenger2->gender = "Female";

$post = array(
    "operatorCode"=>"9",
    "scheduleCode"=>"84W92XC8LOBAF3KZP4",
    "travelDate"=>"2015-03-20",
    "fromStationCode"=>"84",
    "toStationCode"=>"76",
    "boardingPointCode"=>"191933",
    "droppingPointCode"=>"0000",
    "email"=>"*********",
    "mobile"=>"*******",
    "passenger"=>array( $passenger1, $passenger2 )
);

这将产生以下结果

{
   "operatorCode":"9",
   "scheduleCode":"84W92XC8LOBAF3KZP4",
   "travelDate":"2015-03-20",
   "fromStationCode":"84",
   "toStationCode":"76",
   "boardingPointCode":"191933",
   "droppingPointCode":"0000",
   "email":"*********",
   "mobile":"*******",
   "passenger":[
       { 
          "seatNumber":"40",
          "name":"*****",
          "age":"20",
          "gender":"Male"
       },
       {
          "seatNumber":"41",
          "name":"*****",
          "age":"21",
          "gender":"Female"
        }
    ]
}

我也注意到你为什么要使用

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

不会

curl_setopt($ch, CURLOPT_POST, true);

是更好的选择吗?

【讨论】:

  • 感谢您的回复,但仍然是同样的问题,我认为我发送数据的方式存在问题。
  • 我认为问题出在这一行curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=&gt;$data_string));
猜你喜欢
  • 2013-09-09
  • 2021-12-16
  • 2017-01-19
  • 2016-02-10
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
相关资源
最近更新 更多