将cURL转换为PHP cURL

时间:2016-02-27 08:32:27

标签: 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, 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"}}'

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

您可以使用Client URL Library (cURL)-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);

答案 1 :(得分:0)

这是我正在使用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);

?>

我正在尝试使用Onfleet API。显然我需要根据示例curl请求和Onfleet Docs传递params数组中的数组:http://docs.onfleet.com/docs/tasks

您怎么看?

答案 2 :(得分:0)

这是完整的清洁实施:

 <?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);

?>