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

问题描述 投票:0回答:1

这是我用于通过“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文档我不明白。我可以成功地将所有内容添加到“工作产品”中。任何人都可以指出我要么好(卷曲傻瓜)或者可能告诉我应该如何正确发布数据。我不知道如何提问,所以欢迎任何必要的编辑

Sample Post看起来像这样

{  
   "license_key":"123456",
   "customer_id":"74",
   "full_name":"Jack",
   "email_address":"[email protected]",
   "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":"[email protected]",
   "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)

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 protected]',
    '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 protected]',
    '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);

和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;
}
php api curl
1个回答
0
投票

将所有数据放入php数组中,然后使用json_encode()将其编码为json,如下所示:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
    'license_key' => '123456',
    'customer_id' => '74',
    'full_name' => 'Jack',
    'email_address' => '[email protected]',
    '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' => '[email protected]',
    '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格式发送数据,解决方案仍然是相同的,但你必须使用http_build_query(array(...))而不是json_encode(array(...))

© www.soinside.com 2019 - 2024. All rights reserved.