I'm basically processing a HTML form with PHP and then sending it off elsewhere for storage and processing. However I'm having trouble sending array lists through curl. I need to do it in such a way that when it gets to the receiving server it's as if it has come straight from the input form.
I don't receive any errors when using the function if I serialize the arrays, however this makes them unreadable by the server, so they need to keep the post format as if they were coming from a HTML form.
I'm using Kohana but principles of Curl are still the same, here's my code:
$path = "/some/process/path";
$store = "http开发者_运维技巧://www.website.com";
$url = $store . $path;
$screenshots = array();
$screenshots[0] = 'image1.jpg';
$screenshots[1] = 'image2.jpg';
$screenshots[2] = 'image3.jpg';
$videoLinks = array();
$videoLinks[0] = 'video1.wmv';
$videoLinks[1] = 'video2.wmv';
$params = array(
'id' => '12',
'field1' => 'field1text',
'field2' => 'field2text',
'field3' => 'field3text',
'screenshots' => $screenshots,
'videoLinks' => $videoLinks,
);
$options = array(
CURLOPT_HTTPHEADER => array("Accept: application/json"),
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $params,
);
$data = Remote::get($url, $options);
$json = json_decode($data);
Cheers.
CURLOPT_POSTFIELDS => http_build_query($params),
http://php.net/manual/en/function.http-build-query.php
I just wanted to share an alternative to http_build_query()
You can include array inputs with CURLOPT_POSTFIELDS by supplying each subarray item separately.
Instead of...
$videoLinks = array();
$videoLinks[0] = 'video1.wmv';
$videoLinks[1] = 'video2.wmv';
$params = array(
...
'videoLinks' => $videoLinks;
...
);
... do this ...
$params = array(
...
'videoLinks[0]' => 'video1.wmv';
'videoLinks[1]' => 'video2.wmv';
...
);
I'm new to cURL and its PHP version, but as far as I know you can do arrays in your option just fine, just don't forget that if you're sending an array of field=>values then you have to set the Content-Type header to multipart/form-data for proper interpretation. That being said, your array for params is formatted wrong. You have that extra comma after your final array value. That might be what's making it bork.
精彩评论