簡體   English   中英

cURL POST-如何將POSTFIELDS用於此特定查詢

[英]cURL POST - How do you use POSTFIELDS for this specific query

這是我用來通過“ API”發布數據的一段代碼

<?php

    curl_setopt_array($curl, array(
      CURLOPT_URL => "api.ewmjobsystem.com/third-party/add_job",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "license_key=***&customer_id=74&full_name=SystemTest&address=SystemTestAddress&site_address=SystemSiteAddress&short_description=SystemShortDescription&item_id=&item_name=SystemItemName&price=4.99&completion_date=25\04\2019",
      CURLOPT_HTTPHEADER => array(
        "content-type: application/x-www-form-urlencoded",
      ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
        ?>
        <?php
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
        echo $response;



    }
    ?>

現在,我可以使用此代碼執行我想做的所有事情,但是API文檔中有一部分我不理解。 我可以成功地將所有內容添加到“工作產品”中。 誰能指出我的好意(為假人卷曲),或者告訴我如何正確發布數據。 我不知道怎么問這個問題,所以歡迎任何必要的編輯

示例帖子看起來像這樣

{  
   "license_key":"123456",
   "customer_id":"74",
   "full_name":"Jack",
   "email_address":"test@test.com",
   "telephone":"002125254",
   "mobile":"00787787",
   "address":"126 Unit ",
   "city":"Liverpool",
   "county":"MERSEYSIDE",
   "postcode":"CH41 1EP",
   "site_company_name":"Eworks",
   "site_full_name":"K V P",
   "site_telephone":"012121",
   "site_mobile":"0787878",
   "site_email_address":"site@test.com",
   "site_address":"127",
   "site_city":"Liverpool",
   "site_county":"MERSEYSIDE",
   "site_postcode":"CH41 1EP",
   "site_notes":"this is a site notes",
   "customer_ref":"12",
   "wo_ref":"34",
   "po_ref":"56",
   "completion_date":"25\/04\/2017",
   "short_description":"this is short desc",
   "description":"long desc",
   "customer_notes":"customer notes",
   "job_products":[  
      {  
         "item_id":"221",
         "item_name":"TEST:SMOKE OR PRESSURE TEST",
         "item_code":"039018",
         "item_description":"Test:Carry out smoke or pressure test.",
         "cost_price":"21.09",
         "price":"32.44"
      },
      {  
         "item_id":"255",
         "item_name":"WALL:DEMOLISH EXTERNAL WALL",
         "item_code":"101101",
         "item_description":"Wall:Take down external half brick wall and remove spoil.",
         "cost_price":"12.58",
         "price":"19.35"
      }
   ]
}        

所以我終於從他們那里得到了一些示例文件(當告訴他們我將取消每月150英鎊的即時付款)時,他們向我發送了此示例,但仍然Server Error: 500 (Internal Server Error)正常工作Server Error: 500 (Internal Server Error)

example1.php

error_reporting(E_ALL);

include_once('includes.php');

$licence_key = '***'; //Your Licence Key here

//getting the customers
//$response = postRequest($licence_key, 'get_customers');
//print_r($response);

//Add Job
$job_products = [
    [
        "item_id"           => "",
        "item_name"         => "Product A",
        "item_code"         => "039018",
        "item_description"  => "Test:Carry out smoke or pressure test.",
        "cost_price"        => "21.09",
        "price"             => "32.44"
    ],
    [
        "item_id"           => "",
        "item_name"         => "Product B",
        "item_code"         => "039018",
        "item_description"  => "Test:Carry out smoke or pressure test.",
        "cost_price"        => "10",
        "price"             => "50"
    ]
];

$data = [
    'completion_date'       =>  '31/03/2019',
    'customer_id'           =>  1,
    'full_name'             =>  'Full Name',
    'email_address'         =>  'email@email.com',
    'telephone'             =>  '012122212',
    'mobile'                =>  '0787878',
    'address'               =>  'Line 1 address'.chr(10).'Line 2 address',
    'city'                  =>  'City',
    'county'                =>  'County',
    'postcode'              =>  'Postcode',
    'site_company_name'     =>  'Site Company Name',
    'site_full_name'        =>  'Site Contact Name',
    'site_telephone'        =>  '012121212',
    'site_mobile'           =>  '07878787',
    'site_fax'              =>  'Depreciated, not in use',
    'site_email_address'    =>  'email@email.com',
    'site_address'          =>  'Site Line 1 address'.chr(10).'Line 2 address',
    'site_city'             =>  'Site City',
    'site_county'           =>  'Site County',
    'site_postcode'         =>  'Site Postcode',
    'site_notes'            =>  'Site Notes',
    'customer_ref'          =>  'Customer Ref',
    'wo_ref'                =>  'Customer Job Ref',
    'po_ref'                =>  'PO Ref',
    'short_description'     =>  'short description of job',
    'description'           =>  'long description of job',
    'customer_notes'        =>  'Customer notes',
    'job_products'          =>  json_encode($job_products)
];

$response = postRequest($licence_key, 'add_job', $data);
print_r($response);

並包括.php

function postRequest($license_key, $method, $data = []){
    $url                = 'http://api.ewmjobsystem.com/third-party/'; 
    $post_string        = '';

    $data['license_key'] = $license_key;    

    $ch = curl_init(); 

    if(is_array($data) && count($data) > 0){
        $post_string = http_build_query($data);
        curl_setopt($ch, CURLOPT_POST, count($data)); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
    }

    curl_setopt($ch, CURLOPT_URL, $url.$method); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Eworks Manager Client API"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // this line makes it work under https 

    $response = curl_exec($ch); 
    curl_close($ch);

    return $response;
}

將所有數據放入php數組,並使用json_encode()將其編碼為json,如下所示:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
    'license_key' => '123456',
    'customer_id' => '74',
    'full_name' => 'Jack',
    'email_address' => 'test@test.com',
    'telephone' => '002125254',
    'mobile' => '00787787',
    'address' => '126 Unit ',
    'city' => 'Liverpool',
    'county' => 'MERSEYSIDE',
    'postcode' => 'CH41 1EP',
    'site_company_name' => 'Eworks',
    'site_full_name' => 'K V P',
    'site_telephone' => '012121',
    'site_mobile' => '0787878',
    'site_email_address' => 'site@test.com',
    'site_address' => '127',
    'site_city' => 'Liverpool',
    'site_county' => 'MERSEYSIDE',
    'site_postcode' => 'CH41 1EP',
    'site_notes' => 'this is a site notes',
    'customer_ref' => '12',
    'wo_ref' => '34',
    'po_ref' => '56',
    'completion_date' => '25/04/2017',
    'short_description' => 'this is short desc',
    'description' => 'long desc',
    'customer_notes' => 'customer notes',
    'job_products' => array(
        array(
            'item_id' => '221',
            'item_name' => 'TEST:SMOKE OR PRESSURE TEST',
            'item_code' => '039018',
            'item_description' => 'Test:Carry out smoke or pressure test.',
            'cost_price' => '21.09',
            'price' => '32.44',
        ),
        array(
            'item_id' => '255',
            'item_name' => 'WALL:DEMOLISH EXTERNAL WALL',
            'item_code' => '101101',
            'item_description' => 'Wall:Take down external half brick wall and remove spoil.',
            'cost_price' => '12.58',
            'price' => '19.35',
        ),
    ),
)));

job_products特別是數據數組的數組(或當表示為JSON時,它是包含數據的對象的數組)

源代碼是使用以下腳本生成的:

<?php

$json=<<<'JSON'
PUT YOUR JSON HERE
JSON;
$data=json_decode($json,true);
$php_source_code=var_export($data,true);
echo $php_source_code;

順便說一句,到樣例日期,您正在提交JSON,而不是application / x-www-form-urlencoded,所以這是錯誤的:

  CURLOPT_HTTPHEADER => array(
        "content-type: application/x-www-form-urlencoded",
      )

它實際上應該讀

  CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
      )

(並且記錄下來,如果您實際上想要以application/x-www-form-urlencoded -format格式發送數據,則解決方案仍然相同,但是您必須使用http_build_query(array(...))而不是json_encode(array(...))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM