2017-05-16 73 views
1

世界!我在使用OLX的API腳本時遇到了一些麻煩。有人能幫助我嗎?JSON和CURL問題

我正在返回的JSON:

{ 
"access_token": "7b1cb35b4eca28be89174d75baf66e00d2728bf6", 
    "ad_list": [ 
     { 
      "id": "31dasa1d23", 
      "operation": "insert", 
      "category": 1040, 
      "subject": "Título teste - Anúncio OLX - Marília/SP", 
      "body": "Imóvel com Sala, Copa, Cozinha planejada e Gabinete, 3 Dormitórios sendo 1 suíte, Lavanderia com Área de Serviço Coberta, Espaço Gourmet com Churrasqueira e Chuveirão para Ducha, Garagem para 2 Carros, Cerca Elétrica e Interfone. Confira mais sobre o imóvel: http://www.silvioimoveis.com.br/imovel-residencial-professor-antonio-da-silva-penteado-marilia-sp-venda/principal/imovel/principal/5394", 
      "phone": 1433333333, 
      "type": "s", 
      "price": 260000, 
      "zipcode": "17504000", 
      "params":{ 
       "rooms": "3", 
       "garage_spaces": "2" 
      }, 
      "images": [   
       "http://homologacao.silvioimoveis.com.br/fotos/imoveis_internos/5394/2175ea284f81f03.jpg",         
       "http://homologacao.silvioimoveis.com.br/fotos/imoveis_internos/5394/bb0ff30ac2bcbae.jpg" 
      ] 
     } 
     ] 
} 

而我的代碼來創建這個JSON是:

$json = '{ 
     "access_token": "'.$data['access_token'].'", 
     "ad_list": [ 
       { 
        "id": "'.$jsonGerado[0]['id'].'", 
        "operation": "'.$jsonGerado[0]['operation'].'", 
        "category": '.$jsonGerado[0]['category'].', 
        "subject": "'.$jsonGerado[0]['subject'].'", 
        "body": "'.$jsonGerado[0]['body'].'", 
        "phone": '.$jsonGerado[0]['phone'].', 
        "type": "'.$jsonGerado[0]['type'].'", 
        "price": '.$jsonGerado[0]['price'].', 
        "zipcode": "'.$jsonGerado[0]['zipcode'].'", 
        "params": { 
         "rooms": "'.$jsonGerado[0]['params']['rooms'].'", 
         "garage_spaces": "'.$jsonGerado[0]['params']['garage_spaces'].'" 
        }, 
        "images": [ 
         '.$jsonGerado[0]['images']['linkFotoPrincipal'].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][0].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][1].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][2].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][3].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][4].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][5].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][6].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][7].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][8].', 
         '.$jsonGerado[0]['images']['linkDemaisFotos'][9].' 
        ] 
       } 
     ] 
    }'; 

捲曲代碼:

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://apps.olx.com.br/autoupload/import", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "PUT", 
    CURLOPT_POSTFIELDS => $json 
)); 

而且最後,錯誤是:

當我不使用json_encode()在$ JSON變量,我得到了 「NULL值發現......」 而不是

+2

不要手動構建JSON字符串,你幾乎肯定會犯錯誤。製作一個PHP數據結構,然後'json_encode($ it);'首先它必須是UTF-8編碼的,如果你的腳本文件中沒有所有東西_Just Right_即腳本文件必須以UTF- 8等,它不會! – RiggsFolly

+0

在這種情況下,我會做一個json_encode($ json)?我的第一份作品是JSON和CURL,所以我不太瞭解。 – Mathiusso

回答

3

由於RiggsFolly提到 「找到字符串值」,使用json_encode上一個PHP數組或對象,而不是自己構建JSON。這裏是與示例代碼,你提供一個如何做一個例子:

$json_array = array(
     "access_token" => $data['access_token'], 
     "ad_list" => array(
       array(
         "id" => $jsonGerado[0]['id'], 
         "operation" => $jsonGerado[0]['operation'], 
         "category" => $jsonGerado[0]['category'], 
         "subject" => $jsonGerado[0]['subject'], 
         "body" => $jsonGerado[0]['body'], 
         "phone" => $jsonGerado[0]['phone'], 
         "type" => $jsonGerado[0]['type'], 
         "price" => $jsonGerado[0]['price'], 
         "zipcode" => $jsonGerado[0]['zipcode'], 
         "params" => array(
           "rooms" => $jsonGerado[0]['params']['rooms'], 
           "garage_spaces" => $jsonGerado[0]['params']['garage_spaces'] 
         ), 
         "images" => array(
           $jsonGerado[0]['images']['linkFotoPrincipal'], 
           $jsonGerado[0]['images']['linkDemaisFotos'][0], 
           $jsonGerado[0]['images']['linkDemaisFotos'][1], 
           $jsonGerado[0]['images']['linkDemaisFotos'][2], 
           $jsonGerado[0]['images']['linkDemaisFotos'][3], 
           $jsonGerado[0]['images']['linkDemaisFotos'][4], 
           $jsonGerado[0]['images']['linkDemaisFotos'][5], 
           $jsonGerado[0]['images']['linkDemaisFotos'][6], 
           $jsonGerado[0]['images']['linkDemaisFotos'][7], 
           $jsonGerado[0]['images']['linkDemaisFotos'][8], 
           $jsonGerado[0]['images']['linkDemaisFotos'][9] 
         ) 
       ) 
     ) 
); 

然後,您可以做$json = json_encode($json_array);。我希望這有助於!

+1

那麼,這節省了我很多時間和麻煩,所以你得到我的紫外線 – RiggsFolly

+0

幾乎那裏...該代碼返回:{「access_token」:「7b1cb35b4eca28be89174d75baf66e00d2728bf6」,「ad_list」:{「id」:「ab013ca67cf2d50」.. .. }}}。我需要[ad_list]之後的[]:現在的錯誤是[{「property」:「ad_list」,「message」:「找到對象值,但需要一個數組」)] ...我需要類似這樣的東西:{「access_token」:「7b1cb35b4eca28be89174d75baf66e00d2728bf6」,「ad_list」:** [** {「id」:「ab01 3ca67cf2d50」..}} **] **} – Mathiusso

+0

啊,錯過了!進行編輯,嘗試更新代碼。 – UTAlan