Я пытаюсь отправить запрос cURL с PHP на RESTFUL API ExpressPigeon.com.Преобразование терминала cURL Вызов PHP cURL Request
В документации сказано, что это, как создать контакт в списке с помощью Curl команду:
curl -X POST -H "X-auth-key: 00000000-0000-0000-0000-000000000000" \
-H "Content-type: application/json" \
-d '{"list_id": 11,
"contacts": [
{"email": "[email protected]",
"first_name":"John",
"last_name": "Doe"
},
{"email": "[email protected]",
"first_name":"Jane",
"last_name": "Doe"
}] }' \
https://api.expresspigeon.com/contacts
Вот что я сделал:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.expresspigeon.com/contacts');
$fields = array(
'list_id' => $this->list_code,
'contacts' => array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name'])
);
$this->http_build_query_for_curl($fields); //This generates the $this->post_data
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $this->post_data);
$headers = array(
'X-auth-key: '.$this->api_key,
'Content-type: application/json',
'Content-Length: '.strlen(serialize($post))
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = (array) json_decode(curl_exec($ch));
curl_close($ch);
print_r($res);
function http_build_query_for_curl($arrays, $prefix = null)
{
if (is_object($arrays)) {
$arrays = get_object_vars($arrays);
}
foreach ($arrays AS $key => $value) {
$k = isset($prefix) ? $prefix . '['.$key.']' : $key;
if (is_array($value) OR is_object($value) ) {
$this->http_build_query_for_curl($value, $k);
} else {
$this->post_data[$k] = $value;
}
}
}
У меня есть это в результате, хотя:
Array
(
[status] => error
[code] => 400
[message] => required Content-type: application/json
)
Пожалуйста, продемонстрируйте, что вы пробовали. В какой части вызова PHP возникают проблемы с переводом с терминала? – Barmar
Я отредактировал мой код, чтобы показать, что я сделал. Благодарю. – acidpaul
Замечание: Почему вы приписываете результаты json_decode массиву? Это уже сделано для вас. –