doulaozhi6835 2019-02-16 11:08
浏览 214

cURL POST - 如何将POSTFIELDS用于此特定查询

This is a piece of code that i`m using to post data via " 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;



    }
    ?>

now i can do everything i want with this code but there is one part of the API documentation which i do not understand. I can succesfully add everything up to " job products ". Could anyone point me to either good ( curl for dummies ) or maybe show me how i should post the data correctly. I had no idea how t ask the question so any necessary edits are welcomed

Sample Post looks like this

{  
   "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"
      }
   ]
}        

So i have finally got some example files off them ( when told them i will cancel £150 a month im paying them ) and they have sent me this as an example but still doesnt work 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);

and includes.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;
}
  • 写回答

1条回答 默认 最新

  • doujian7132 2019-02-16 13:33
    关注

    put all the data in a php array, and simply encode it to json with json_encode(), like this:

    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 specifically is an array of arrays of data (or when represented as JSON, it's an array of objects containing data)

    the source code was generated with this script:

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

    and by the way, going by your sample date, you're submitting JSON, not application/x-www-form-urlencoded , so this is wrong:

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

    it should actually read

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

    (and for the record, IF you actually waned to send the data in application/x-www-form-urlencoded-format, the solution would still be the same, but you'd have to use http_build_query(array(...)) instead of json_encode(array(...)) )

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀