It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format
<?php
$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36];
$defaults = array(
CURLOPT_URL => 'http://myremoteservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
?>
This produce the following post header:
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="name"
Jhon
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="surnname"
Doe
--------------------------fd1c4191862e3566
Content-Disposition: form-data; name="age"
36
--------------------------fd1c4191862e3566--
Setting CURLOPT_POSTFIELDS as follow produce a standard post header
CURLOPT_POSTFIELDS => http_build_query($params),
Which is:
name=John&surname=Doe&age=36
This caused me 2 days of debug while interacting with a java service which was sensible to this difference, while the equivalent one in php got both format without problem.Основен пример за curl
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Основен пример за curl
Референца за `curl.examples-basic.php` со подобрена типографија и навигација.
Основен пример за curl
Откако ќе го компајлирате PHP со поддршка за cURL, можете да почнете да ги користите функциите на cURL. Основната идеја зад функциите на cURL е дека иницирате cURL сесија користејќи го curl_init(), потоа можете да ги поставите сите ваши опции за пренос преку curl_setopt(), потоа можете да ја извршите сесијата со curl_exec(). Еве пример што ги користи функциите на cURL за да ја преземе почетната страница на example.com во датотека:
Пример #1 Користење на PHP модулот cURL за преземање на почетната страница на example.com
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
if(curl_error($ch)) {
fwrite($fp, curl_error($ch));
}
fclose($fp);
?>