简体   繁体   中英

crawling with curl - just cant get it right

So i am pullin item data and the rest

I am unable to get ItemDataCosts

I am pulling al the json thats not an array inside an array- but item data cost i am unable to get

If any body can point me in the correct direction

with

echo "<br>ItemDataCosts start<br>"; 

foreach($result->ItemData->ItemDataCosts as $ItemDataCosts) {
echo $ItemDataCosts->Seq.'Seq<br>';
echo $ItemDataCosts->FUnitPrice.'FUnitPrice<br>';
echo $ItemDataCosts->Currencies_Seq.'Currencies_Seq<br>'; 
}
echo "<br>ItemDataCosts end<br>";   

This is what is returned from json

 "ItemData"
  ]=>array(4){
    [
      0
    ]=>array(46){
  [
    "BookStatus"
  ]=>int(3)[
    "BookNotes"
  ]=>array(2){
    [
      "SupplierReference"
    ]=>string(0)""[
      "Details"
    ]=>string(85)" Cancelled on: 7/23/2018 12:58:07 PM BRBK1H Booked on: 3/9/2018 1:15:23 PM "
  }[

    "Allocated"
  ]=>bool(false)[
    "StartDate"
  ]=>string(20)"2018-08-05T14:00:00Z"[
    "CostPriceCompo"
  ]=>NULL[
    "GTABookingHeaders"
  ]=>array(0){

  }[
    "ProductTariff"
  ]=>NULL[
    "QuoteProp"
  ]=>NULL[
    "ItemDataCosts"
  ]=>array(1){
    [
      0
    ]=>array(19){
      [
        "Seq"
      ]=>float(720949)[
        "ItemData_Seq"
      ]=>float(140)[
        "UnitPrice"
      ]=>float(819)
    }
  }

and i get the json with below php curl - the output is wat i got then i take that and paste in http://jsonviewer.stack.hu and then format to view the fields

error_reporting(E_ALL);
ini_set('display_errors', 1);

$url = 'https://my.travpro.cloud/api//costings/getcosting/*****';

$auth = base64_encode('paul.******:******3');
$postfields = http_build_query( array(
    'grant_type' => 'client_credentials'));
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => $postfields,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Content-Type: application/x-www-form-urlencoded",
    "Authorization: Basic $auth"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($response);

ItemData (according to your array-ified JSON) is array. Using -> will not work on it, but $result->ItemData['ItemDataCosts'] should do. so this should work:

$result = json_decode($response);
echo "<br>ItemDataCosts start<br>";
$itemData = $result->{'ItemData'};
foreach($itemData as $ItemData) {
    foreach($itemData as $item) {
        $itemDataCosts = $item->{'ItemDataCosts'};
        foreach($itemDataCosts as  $itemDataCost) {
            echo $itemDataCost->{'Seq'} . 'Seq<br>';
            echo $itemDataCost->{'FUnitPrice'} . 'FUnitPrice<br>';
            echo $itemDataCost->{'Currencies_Seq'} . 'Currencies_Seq<br>';
        }
    }
}
echo "<br>ItemDataCosts end<br>"; 

found the answer by googling a lot here is what works

$this_data = $result->ItemData;

foreach($this_data as $key){
    foreach($key->ItemDataCosts as $values){
        echo '<pre>';
        echo $values->ItemData_Seq . ' ItemData Sequence';
        echo $values->FUnitPrice . ' Unit Price';
        echo '</pre>';
        echo '<br/>';
    }
}
die();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM