Tricky things we learned about curl

16 views
Skip to first unread message

Jochen Daum

unread,
Aug 2, 2011, 7:13:34 PM8/2/11
to PHPUG
Hi,

we just went though a long journey of curl gotchas here and I would
not wish this onto anyone else. Here are some findings that may help
people:

= Tricky things we learned about curl =

== CURLOPT_POST before CURLOPT_POSTFIELDS ==

We're not sure if it sometimes causes a problem, but advice is that
CURLOPT_POST should be before CURLOPT_POSTFIELDS. The other way around
may not work.

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postThrough);

== CURLOPT_HEADERFUNCTION does not support object or class callback in
some version ==

libcurl/7.18.2 on Debian Lenny, PHP 5.2.6 backported only seems to
support functions in global space, whereas 7.19.7, Ubuntu 10.10, 5.3.2
seems to support both array('class', 'function') (if static keyword
set!) and array($this, 'function').

== Sending Files ==

You need to use curl_setopt($ch, CURLOPT_POSTFIELDS, $postThrough);
with $postThrough being an array, otherwise the formtype is not
multipart.

== Sending arrays in POST ==

To send

$x = array('value' => array('a' => 1, 'b' => 2));

You need to convert the arrays into "square brace strings":

function arraysToBrackets($array, $prefix = '', $level=0){

$resarr = array();
if (0 && $prefix && $level > 1){
$prefix = '['.$prefix.']';
}
foreach ($array as $field => $data){
if (is_array($data)){
$resarr = array_merge($resarr, arraysToBrackets($data,
($prefix?$prefix.'['.$field.']':$field),$level+1));
}else{
if ($level > 0){
$resarr[$prefix.'['.$field.']'] = $data;
}else{
$resarr[$prefix.$field] = $data;
}
}
}
return $resarr;
}


== Passing forward files in arrays ==

When you pass forward files that are in an array, you need to rejig
the format of the file array that comes in, so it matches the format
going out:

if ($_FILES){

foreach ($_FILES as $file => $details){
if (is_array($details['name'])){
foreach ($_FILES[$file]['name'] as $file2 => $details2){
if ($_FILES[$file]['tmp_name'][$file2]){
$postThrough[$file.'['.$file2.']'] =
'@'.$_FILES[$file]['tmp_name'][$file2].';filename='.$_FILES[$file]['name'][$file2];
}
}
}else{
if ($details['tmp_file']){
$postThrough[$file] = '@'.$details['tmp_file'];
}
}
}

== headers passed to CURLOPT_HEADERFUNCTION include the newline at the
end of the header ==

When you process these headers and possibly pass them on to another
site through curl, make sure the newline is removed, otherwise POST
data will not reach the curl'd site

== debugging proxy ==

To debug issues an easy to use proxy is [http://portswigger.net/burp/ BurpSuite]

Kind Regards,

Jochen Daum

"There is no shortcut to anywhere worth going" - Beverly Sills

P.S.: Missed our newsletter? http://eepurl.com/ejRsg

Automatem Ltd
Phone: 09 630 3425
Mobile: 021 567 853
Email: j...@automatem.co.nz
Website: www.automatem.co.nz
http://nz.linkedin.com/in/automatem
http://twitter.com/automatem

Sarah King

unread,
Aug 2, 2011, 7:58:09 PM8/2/11
to nzp...@googlegroups.com
That's a good set of hints. Thanks

Sarah King
@itamer
p: 09 815 8642
m: 0273 711 298
skype: sarah.king





--
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to nzp...@googlegroups.com
To unsubscribe, send email to
nzphpug+u...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages