...

View Full Version : Help with PHP cURL request



t-bag
09-28-2010, 09:51 AM
Hi guys,

I was wondering if you could help me. I have this code which should send a call to a RESTful web service (which creates a new user in a database). My code is executing without any errors (that I can see) however the user doesn't get created.


$url = 'http://127.0.0.1:85/AccountCreator.ashx';

$curl_post_data = array(
'$companyName' =>$companyName,
'$mainContact' =>$mainContact,
'$telephone1' =>$telephone1,
'$email' => $email,
'$contact2' => $contact2,
'$telephone2' => $telephone2,
'$email2' => $email2,
'$package' => $package
);

foreach($curl_post_data as $key=>$value) {$fields_string.=$key. '=' .$value.'&';
}
rtrim($fields_string, '&');
//die("Test: ".$fields_string);

$ch = curl_init();

curl_setopt ($ch, CURLOPT, $url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);

I'm fairly new to PHP also, so I just can't see where I'm doing wrong. I have access to the server also - is there anything I can check on that which would indicate if the web service is being called or not?

I've also read this in http://php.net/manual/en/function.curl-setopt.php...


CURLOPT_POSTFIELDS: The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

I don't think my PHP document is set to multipart/form-data - Would this affect it?

t-bag
09-28-2010, 11:04 AM
Hi guys,

I was wondering if you could help me. I have this code which should send a call to a RESTful web service (which creates a new user in a database). My code is executing without any errors (that I can see) however the user doesn't get created.


$url = 'http://127.0.0.1:85/AccountCreator.ashx';

$curl_post_data = array(
'$companyName' =>$companyName,
'$mainContact' =>$mainContact,
'$telephone1' =>$telephone1,
'$email' => $email,
'$contact2' => $contact2,
'$telephone2' => $telephone2,
'$email2' => $email2,
'$package' => $package
);

foreach($curl_post_data as $key=>$value) {$fields_string.=$key. '=' .$value.'&';
}
rtrim($fields_string, '&');
//die("Test: ".$fields_string);

$ch = curl_init();

curl_setopt ($ch, CURLOPT, $url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);

I'm fairly new to PHP also, so I just can't see where I'm doing wrong. I have access to the server also - is there anything I can check on that which would indicate if the web service is being called or not?

I've also read this in http://php.net/manual/en/function.curl-setopt.php...



I don't think my PHP document is set to multipart/form-data - Would this affect it?

A typo has been pointed out to me. curl_setopt ($ch, CURLOPT, $url);
should be curl_setopt ($ch, CURLOPT_URL, $url);

This gives me a http 400 error but at least this is an error to go on!

dominicdinada
09-30-2010, 07:34 AM
$url = 'http://127.0.0.1:85/AccountCreator.ashx';

$curl_post_data = array(
"companyName" =>"$companyName",
"mainContact" =>"$mainContact",
"telephone1" =>"$telephone1",
"email" => "$email",
"contact2" => "$contact2",
"telephone2" => '$telephone2',
"email2" => "$email2",
"package" => "$package"
);


curl_setopt ($ch, CURLOPT_POSTFIELDS, $curl_post_data);
$result = curl_exec($ch);
curl_close($ch);



Without Seeing the post form or anything you shouldn't need to run the post array through a foreach loop, But the variables you are posting need to match the value of the form. so


<input type="text" value="email" ... >

you are pulling the value "email" not $email. this should give you a start as i just had a huge headache with curl

dominicdinada
09-30-2010, 07:39 AM
Also some basic handling to see what errors pop up



if (curl_errno($ch)) {
echo "Erro CURL: " . curl_error($ch);
} else { echo $response; }
curl_close($ch);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum