使用php curl的JSON POST数据

时间:2018-03-31 08:32:03

标签: php json curl

这是来自url(http://demoerp.rawntech.com/demoerp/org.openbravo.service.json.jsonrest/BusinessPartner)的json数据:

{
        "response": {
        "data": [
        {
            "_identifier": "Be Soft Drinker, Inc.",
            "_entityName": "BusinessPartner",
            "$ref": "BusinessPartner/FF2EC1D0E28E4F85BE85532ADD556772",
            "id": "FF2EC1D0E28E4F85BE85532ADD556772",
            "client": "23C59575B9CF467C9620760EB255B389",
            "client$_identifier": "F&B International Group",
            "organization": "2E60544D37534C0B89E765FE29BC0B43",
            "organization$_identifier": "F&B US, Inc.",
            "active": true,
            "creationDate": "2013-07-04T23:45:43-04:00",
            "createdBy": "0",
            "createdBy$_identifier": "System - ",
            "updated": "2015-11-08T04:55:36-05:00",
            "updatedBy": "0",
            "updatedBy$_identifier": "System - ",
            "searchKey": "US-SP/0003",
            "name": "Be Soft Drinker, Inc.",
            "name2": null,
            "description": "Supplier for soft drinks and waters",
            "summaryLevel": false,
            "businessPartnerCategory": "1B7DCAD27AF64D16ADCFB4EBE8600078",
            "businessPartnerCategory$_identifier": "Supplier",
            "oneTimeTransaction": false,
            "potentialCustomer": false,
            "vendor": true,
            "customer": false,
            "employee": false,
            "isSalesRepresentative": false,
            "referenceNo": null,
            "dUNS": null,
            "uRL": null,
            "language": "en_US",
            "language$_identifier": "English (USA)",
            "taxID": "JH812637",
            "taxExempt": false,
            "invoiceSchedule": null,
            "valuation": null,
            "volumeOfSales": null,
            "noOfEmployees": null,
            "nAICSSIC": null,
            "dateOfFirstSale": "2011-02-12",
            "acquisitionCost": 0,
            "expectedLifetimeRevenue": 0,
            "lifetimeRevenueToDate": 0,
            "share": null,
            "formOfPayment": null,
            "creditLimit": 0,
            "creditUsed": -34100,
            "paymentTerms": null,
            "priceList": null,
            "printDiscount": false,
            "orderDescription": null,
            "orderReference": null,
            "pOFormOfPayment": null,
            "purchasePricelist": "C17357617FD347EF8A64A7BB98CF586F",
            "purchasePricelist$_identifier": "Be Soft Drinker Price List",
            "pOPaymentTerms": "66BA1164A7394344BB9CD1A6ECEED05D",
            "pOPaymentTerms$_identifier": "30 days",
            "numberOfCopies": null,
            "greeting": null,
            "invoiceTerms": "I",
            "deliveryTerms": null,
            "deliveryMethod": null,
            "salesRepresentative": null,
            "partnerParent": null,
            "creditStatus": "O",
            "forcedOrg": null,
            "pricesShownInOrder": true,
            "invoiceGrouping": "000000000000000",
            "maturityDate1": null,
            "maturityDate2": null,
            "maturityDate3": null,
            "operator": false,
            "uPCEAN": null,
            "salaryCategory": null,
            "invoicePrintformat": null,
            "consumptionDays": 150,
            "bankAccount": null,
            "taxCategory": "524533211CFA428497D736B31D597097",
            "taxCategory$_identifier": "Exempt",
            "pOMaturityDate1": null,
            "pOMaturityDate2": null,
            "pOMaturityDate3": null,
            "transactionalBankAccount": null,
            "sOBPTaxCategory": null,
            "fiscalcode": null,
            "isofiscalcode": null,
            "incotermsPO": null,
            "incotermsSO": null,
            "paymentMethod": null,
            "pOPaymentMethod": "15263EF498404ED3BEA2077023A4B68C",
            "pOPaymentMethod$_identifier": "Wire Transfer",
            "account": null,
            "pOFinancialAccount": "2C059762760F4F3D8A91342ED988DEB8",
            "pOFinancialAccount$_identifier": "Bank - Account 1",
            "customerBlocking": false,
            "vendorBlocking": false,
            "paymentIn": false,
            "paymentOut": true,
            "salesInvoice": true,
            "purchaseInvoice": true,
            "salesOrder": true,
            "purchaseOrder": true,
            "goodsShipment": true,
            "obcwacWidgetCard": null,
            "goodsReceipt": false,
            "cashVAT": false,
            "opcrmBpEmail": null,
            "opcrmBpType": null,
            "opcrmBpIndustry": null,
            "currency": "100",
            "currency$_identifier": "USD",
            "opcrmBpAnnualRevenue": null,
            "opcrmBpEmployees": null,
            "opcrmBpSicCode": null,
            "opcrmBpTickerSymbol": null,
            "opcrmBpOwnership": null,
            "opcrmBpRating": null,
            "opcrmBpCCampaign": null,
            "opcrmBpAssignedTo": null,
            "opcrmBtnCreateopp": null,
            "opcrmBtnCreateact": null,
            "opcrmDoctype": null,
            "opcrmIsposdefault": null,
            "setNewCurrency": false,
            "recordTime": 1522484605908 
     }],
     "status": 0,
     "totalRows": 83,
     "startRow": 0,
     "endRow": 82
}

我想尝试使用php从网址检索json发布数据。这是我的PHP脚本。我无法直接发布数据,因为还有另一个对象响应。

<?php   
        $url = 'http://username:password@demoerp.rawntech.com/demoerp/org.openbravo.service.json.jsonrest/BusinessPartner';

        //Initiate cURL.
        $ch = curl_init($url);

        //The JSON data.
        $jsonData = array(
            'entityName' => 'BusinessPartner',
            'id' => 'A6750F0D15334FB890C254369AC750A8',
            'name' => 'karim'
        );

        //Encode the array into JSON.
        $jsonDataEncoded = json_encode($jsonData);

        //Tell cURL that we want to send a POST request.
        curl_setopt($ch, CURLOPT_POST, 1);

        //Attach our encoded JSON string to the POST fields.
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

        //Set the content type to application/json
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

        //Execute the request
        $result = curl_exec($ch);

        if(curl_errno($ch)){
                throw new Exception(curl_error($ch));
            }
            return $result;
        ?>

错误显示在我的浏览器上:

{
   "response":{
      "status":-1,
      "error":{
         "message":"org.codehaus.jettison.json.JSONException: JSONObject[\"data\"] not found.",
         "messageType":"Error",
         "title":""
      },
      "totalRows":0
   }
}

1 个答案:

答案 0 :(得分:0)

谢谢,错误解决。我分享我的代码。

$url = 'http://username:password@demoerp.rawntech.com/demoerp/org.openbravo.service.json.jsonrest/BusinessPartner';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
    'entityName' => 'BusinessPartner',
    'id' => 'A6750F0D15334FB890C254369AC750A8',
    'name' => 'khayer'
);
$data_string = json_encode(array("data" =>$jsonData));

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);

echo "$result";