views:

19

answers:

2

Hi guys,

I have this curl request below, which was successfully troubleshooted in another post. Right now, my PHP seems to work through this code without any errors, moves onto the next part of the IF statement and sends the confirmation email. It just doesn't update the database as it should from the web service. I will have to email the creator of the web service if this does not work but I just want to be sure that the code is fairly solid before I do this. Any one have any ideas? Here is the code:

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


$curl_post_data = array(
    'companyName' =>urlencode($companyName),
    'mainContact' =>urlencode($mainContact),
    'telephone1' =>urlencode($telephone1),
    'email' => urlencode($email),
    'contact2' => urlencode($contact2),
    'telephone2' => urlencode($telephone2),
    'email2' => urlencode($email2),
    'package' => urlencode($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, count($curl_post_data));
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);
+1  A: 

The CURLOPT_POSTFIELDS option accepts an associative array of POST-data. Probably better to use that one rather than to construct the query string yourself so you got someone else to blame when it blows up.

PHP Manual:

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.

Alexander Sagen
+1  A: 

Firstly, what is the problem? It would be easier to troubleshoot it if you explained exactly what the failure in the code was. Secondly, there are a couple of odd things you are doing in this code:

I don't see why you are doing

curl_setopt ($ch, CURLOPT_POST, count($curl_post_data));

CURLOPT_POST requires a boolean (true/false) setting. You should set it to true.

Secondly, you don't need to encode CURLOPT_POSTFIELDS manually. Make an array and let cURL deal with it internally:

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

These might not fix the problem, but they may help.

lonesomeday
Yes. This might not make a difference (it's likely that the number is converted into a boolean, so any positive integer is interpreted as true), but it might be that non-boolean values are ignored.
lonesomeday
Thanks for the reply. I have changed it to true, and will test.
TaraWalsh
Hi lonesomeday, you were right about encoding, my email address was not displaying correctly before but it is now since I removed this. I have updated my post to state the problem. My problem is that this peice of code should call a web service to create a new user in the database, however this is not being done. I however, have not seen the code for the web service as it was written by someone else so I am not sure where the problem lies. That's why I wanted to eliminate the above code as a problem
TaraWalsh