在php中使用Curl发布数据

时间:2018-02-22 08:50:05

标签: php curl

我想将两个对象数组发布到API,但它显示参数的错误缺失值:productDC

似乎我必须传递ProductDc和buyerDC以及他们的数据。为此,我也尝试了

 $data_string = array({ProductDc"product_name" => "Electronics","price" => "200 usd"},
    buyerDC{"buyer_name" => "john mark","address" => "17 more strret"}); 

下面是信息

1。) ProductDC 将其作为对象数组传递 ** PRODUCT_NAME(字符串)

价格(字符串)**

2。) buyerDC 将其作为对象数组传递 ** BUYER_NAME(字符串)

地址(字符串**)

这是我的代码

<?php

$data_string = array("product_name" => "Electronics","price" => "200 usd",
"buyer_name" => "john mark","address" => "17 more strret"); 


$data = json_encode($data_string); 
//$data = $data_string; 
$curl = curl_init();

curl_setopt_array($curl, array( 
CURLOPT_URL => "myapi.com", 
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_ENCODING => "", 
CURLOPT_MAXREDIRS => 10, 
CURLOPT_TIMEOUT => 30, 
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
CURLOPT_CUSTOMREQUEST => "POST", 
CURLOPT_POSTFIELDS => "$data", 
CURLOPT_HTTPHEADER => array( 
"accept: application/json",  
"content-type: application/json; charset=utf-8" 
), 
));

$response = curl_exec($curl); 
$err = curl_error($curl);

curl_close($curl);

if ($err) { 
echo "cURL Error #:" . $err; 
} else { 
echo $response; 

}


?>

1 个答案:

答案 0 :(得分:1)

考虑以下您提供的信息,如果他们想要的数据格式为 -

1。)ProductDC应该是一个包含 -

的对象数组

PRODUCT_NAME(字符串)

价格(字符串)

2。)buyerDC应该是包含 -

的对象数组

BUYER_NAME(字符串)

地址(字符串)

可能他们期望通过更改$ data_string值获得不同形式的数据,如下所示 -

selectionAccordion {
            &:hover, &:focus {
                color: White;
               background-color:Blue;
            }
        }
        .directive('accordion', function () {
              return {
                  restrict: 'EA',
                  replace: true,
                  transclude: true,
                  template: '<div data-ng-transclude=""></div>',
                  controller: function () {
                      var Spots = [];
                      this.Open = function (selected_Spot) {
                          angular.forEach(Spots, function (Spot) {
                              if (selected_Spot != Spot)
                                  Spot.showMe = false;
                          });
                      };
                      this.addSpot = function (Spot) {
                         Spots.push(Spot);
                      };
                  }
              };
          })
          .directive('spotcam', function () {
                    return {
                        restrict: 'EA',
                        replace: true,
                        transclude: true,
                        require: '^accordion',
                        scope: { title: '@' },
                        template: '<div>' +


          '<div class="title selectedAccordionStyle ng-if="true"><a class=" more-less glyphicon glyphicon-plus" ng-class="{true: \'glyphicon glyphicon-minus\', false:\'glyphicon glyphicon-plus\'}[showMe]"  data-ng-click="toggle()"></a>{{title}}</div>' +
                    '<div class="body" data-ng-show="showMe" data-ng-transclude=""></div>' +
                        '</div>',
                        link: function (scope, element, attrs, accordionController) 
                            scope.showMe = false;
                            accordionController.addSpot(scope);
                            scope.toggle = function () {
                                scope.showMe = !scope.showMe;
                                accordionController.Open(scope);
                        } }  }
         });

将$ data_string值更改为我在上面发布的值并检查。