这是我用来通过“ 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)
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);
和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;
}
答案 0 :(得分:0)
将所有数据放入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
格式发送数据,则解决方案仍然相同,但是您必须使用http_build_query(array(...))
而不是{{ 1}})