将cURL转换为PHP cURL
我正在尝试将此curl命令转换为php:将cURL转换为PHP cURL,php,api,curl,Php,Api,Curl,我正在尝试将此curl命令转换为php: curl -X POST "https://onfleet.com/api/v2/tasks" -u "456798b4f32516d4c75cf80bacc6f32a:" -d '{"destination":{"address":{"unparsed":"2829 Vallejo St, SF, CA, USA"},"notes":"Small green door by garage door has pin pad,
curl -X POST "https://onfleet.com/api/v2/tasks" -u "456798b4f32516d4c75cf80bacc6f32a:" -d '{"destination":{"address":{"unparsed":"2829 Vallejo St, SF, CA, USA"},"notes":"Small green door by garage door has pin pad, enter *4821*"},"recipients":[{"name":"Blas Silkovich","phone":"+16505554481","notes":"Knows Neiman, VIP status."}],"completeAfter":1455151071727,"notes":"Order 332: 24oz Stumptown Finca El Puente, 10 x Aji de Gallina Empanadas, 13-inch Lelenitas Tres Leches","autoAssign":{"mode":"distance"}}'
有人能帮忙吗?下面是我使用Curl+PHP所做的:
<?php
//Onfleet API credentials
$username = 'XXXX';
$api = '456798b4f32516d4c75cf80bacc6f32a';
// Apply custom curl request to url
$url = "https://onfleet.com/api/v2/tasks";
// Give parameters
$fields = array(
'destination'=> '2829 Vallejo St, SF, CA, USA',
'recipients'=> 'Blas Silkovich'
);
// intiate curl request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($ch,CURLOPT_GETFIELDS,$params);
// set the number of post fields
curl_setopt($ch,CURLOPT_POST, $fields);
// set the fields
//execute post
$result = curl_exec($ch);
print_r($result);
//close connection
curl_close($ch);
?>
我正在尝试使用onfleetapi。显然,我需要根据示例curl请求和Onfleet文档在params数组中传递一个数组:
你觉得怎么样?下面是我使用Curl+PHP所做的:
<?php
//Onfleet API credentials
$username = 'XXXX';
$api = '456798b4f32516d4c75cf80bacc6f32a';
// Apply custom curl request to url
$url = "https://onfleet.com/api/v2/tasks";
// Give parameters
$fields = array(
'destination'=> '2829 Vallejo St, SF, CA, USA',
'recipients'=> 'Blas Silkovich'
);
// intiate curl request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($ch,CURLOPT_GETFIELDS,$params);
// set the number of post fields
curl_setopt($ch,CURLOPT_POST, $fields);
// set the fields
//execute post
$result = curl_exec($ch);
print_r($result);
//close connection
curl_close($ch);
?>
我正在尝试使用onfleetapi。显然,我需要根据示例curl请求和Onfleet文档在params数组中传递一个数组:
你觉得怎么样?你可以使用。-u
正在设置用户名(无密码),因此请使用CURLOPT_USERPWD
-d
设置数据,以便使用CURLOPT_POSTFIELDS
:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onfleet.com/api/v2/tasks');
curl_setopt($ch, CURLOPT_USERPWD, 'xxxxxxxxxxxx:');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"2829 Vallejo St, SF, CA, USA"},"notes":"Small green door by garage door has pin pad, enter *4821*"},"recipients":[{"name":"Blas Silkovich","phone":"+16505554481","notes":"Knows Neiman, VIP status."}],"completeAfter":1455151071727,"notes":"Order 332: 24oz Stumptown Finca El Puente, 10 x Aji de Gallina Empanadas, 13-inch Lelenitas Tres Leches","autoAssign":{"mode":"distance"}}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
您可以使用。-u
正在设置用户名(无密码),因此请使用CURLOPT_USERPWD
-d
设置数据,以便使用CURLOPT_POSTFIELDS
:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onfleet.com/api/v2/tasks');
curl_setopt($ch, CURLOPT_USERPWD, 'xxxxxxxxxxxx:');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"2829 Vallejo St, SF, CA, USA"},"notes":"Small green door by garage door has pin pad, enter *4821*"},"recipients":[{"name":"Blas Silkovich","phone":"+16505554481","notes":"Knows Neiman, VIP status."}],"completeAfter":1455151071727,"notes":"Order 332: 24oz Stumptown Finca El Puente, 10 x Aji de Gallina Empanadas, 13-inch Lelenitas Tres Leches","autoAssign":{"mode":"distance"}}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
以下是完整的clean实现:
<?php
//Onfleet API credentials
$username = 'xxxxxx';
$api = 'xxxxxxxxxxxxxxxxxxxxx';
$url = "https://onfleet.com/api/v2/tasks";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"2829 Vallejo St, SF, CA, USA"},"notes":"Small green door by garage door has pin pad, enter *4821*"},"recipients":[{"name":"Blas Silkovich","phone":"+16505554481","notes":"Knows Neiman, VIP status."}],"completeAfter":1455151071727,"notes":"Order 332: 24oz Stumptown Finca El Puente, 10 x Aji de Gallina Empanadas, 13-inch Lelenitas Tres Leches","autoAssign":{"mode":"distance"}}');
$result = curl_exec($curl);
curl_close($ch);
print_r($result);
?>
以下是完整的干净实现:
<?php
//Onfleet API credentials
$username = 'xxxxxx';
$api = 'xxxxxxxxxxxxxxxxxxxxx';
$url = "https://onfleet.com/api/v2/tasks";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, '{"destination":{"address":{"unparsed":"2829 Vallejo St, SF, CA, USA"},"notes":"Small green door by garage door has pin pad, enter *4821*"},"recipients":[{"name":"Blas Silkovich","phone":"+16505554481","notes":"Knows Neiman, VIP status."}],"completeAfter":1455151071727,"notes":"Order 332: 24oz Stumptown Finca El Puente, 10 x Aji de Gallina Empanadas, 13-inch Lelenitas Tres Leches","autoAssign":{"mode":"distance"}}');
$result = curl_exec($curl);
curl_close($ch);
print_r($result);
?>
您需要使用或者如果您不想使用新扩展,您可以使用您需要使用或者如果您不想使用新扩展,您可以使用您只需要对$field变量进行JSON编码-您是否尝试过编码:curl_setopt($ch,CURLOPT_POST,JSON_encode($fields));您只需要对$field变量进行JSON编码——您是否尝试过编码:curl_setopt($ch,CURLOPT_POST,JSON_encode($fields));谢谢jksn.co,它在工作:)谢谢jksn.co,它在工作:)