什么是LIBCURL相当于-d?

时间:2016-05-04 12:59:11

标签: php curl libcurl

在LIBCURL for PHP中,什么是基本的curl -d?

这是我的基本CURL:我需要在PHP中为LIBCURL格式化它:

curl -u username:password -H "Content-Type:application/json" -X POST -d '[{"phoneNumber":"12135551100","message":"Hello World!"}]' "https://api.example.com/v2/texts"

我尝试过使用CURLOPT_WRITEFUNCTIONCURLOPT_WRITEDATA,但似乎无法满足我的要求。

2 个答案:

答案 0 :(得分:2)

所需选项为CURLOPT_POSTFIELDS。规范与libcurl引用相同。

curl_setopt引用中有一些PHP示例。最简单的方法是以下示例:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2");

答案 1 :(得分:0)

我的端点需要数组形式的数据,所以这很有效。

function doPost($url, $user, $password, $params = array()) {
    $authentication = 'Authorization: Basic '.base64_encode("$user:$password");
    $http = curl_init($url);
    curl_setopt($http, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($http, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($http, CURLOPT_URL, $url);
    curl_setopt($http, CURLOPT_POST, true);
    curl_setopt($http, CURLOPT_POSTFIELDS, $params);
    curl_setopt($http, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json', $authentication));
    return curl_exec($http);
}

我的数组格式如下:

$params = '[{"phoneNumber":"' . $ToNumber . '","message":"' . $msg . '"}]';
相关问题