带有 php curl 的 JSON POST 数据

RK-罗汉

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

{
        "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 从 url 检索 json post 数据。这是我的 php 脚本。我不能直接发布数据,因为还有另一个对象响应。

<?php   
        $url = 'http://username:[email protected]/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
   }
}
RK-罗汉

谢谢,错误slove。我分享我的代码。

$url = 'http://username:[email protected]/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";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章