views:

81

answers:

3

Hi,

I am trying to post to a REST service using PHP cURL but I'm after running into a bit of difficulty (this being that I've never used cURL before!!).

I've put together this code:

  <?php
error_reporting(E_ALL);
if ($result == "00")
{

$url = 'http://127.0.0.1/xxxxxx/AccountCreator.ashx'; /*I've tried it a combination of ways just to see which might work */

$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);

Following this, my code sends an email and performs an IF statement. I know this works okay, I only started running into trouble when I tried to insert this cURL request.

I've tried this however it doesn't run. As I am integrating with payment partners, it just says:

Your transaction has been successful but there was a problem connecting back to the merchant's web site. Please contact the merchant and advise them that you received this error message. Thank you.

The exact error that was received was a HTTP 500 error.

Thanks.

+3  A: 

foreach($curl_post_data as $key=>value) {$fields_string .=$key. '=' .value.'&';

value here is missing a dollar i guess

foreach($curl_post_data as $key => $value) {$fields_string .=$key. '=' .$value.'&';

have you tried die($fields_string); to see what are you actually sending to the merchant?

sathia
I tried inserting the dollar symbol where it was missing and still no joy. Thanks anyway.
TaraWalsh
have you added both dollars sign? what die($fields_string); prints?
sathia
@sathia, yup, both dollars sign. I didn't try die($fields_string); prints, where should I insert this?
TaraWalsh
foreach($curl_post_data as $key=>$value) {$fields_string .=$key. '=' .value.'}rtrim($fields_string, ' die($fields_string);
sathia
Hi sathia, I inserted that line however nothing comes up. I think whoever set up the server however turned off error reporting. Is there a way of turning this on for just this file?
TaraWalsh
put this as the first line of your code then: error_reporting(E_ALL); and this as your die output: die("Test: ".$fields_string);
sathia
@sathia...there was still no output from this.
TaraWalsh
post all the code of that page, let's see what's wrong here
sathia
I've just posted a larger snippet of my code so you can have a look sathia. I did not post beyond the curl method because it's basically just a large email I send to users, and an IF statement. I have tested prior to inserting curl and it worked okay so I'm confident it is curl that is throwing it off.
TaraWalsh
http://pastebin.com/zBVhVh3Y
sathia
+1  A: 

First of all: are you testing locally? Because that IP you're using is not a valid server address.

The constant to set the URL is called CURLOPT_URL:

curl_setopt ($ch, CURLOPT_URL, $url);

Also CURLOPT_POST must be true or false ( http://php.net/curl_setopt ), not a number (except for 1 maybe):

curl_setopt ($ch, CURLOPT_POST, true);

Here's some POST sample code: http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code

DanMan
@DanMan, thanks for the reply. No I am testing on the server. The web service I am connecting to is on a different port for now, which represents the :00 at the end of the IP address. This was one of the things I was unsure about - if I could use the port number?
TaraWalsh
I suppose it shoud work, but you can try setting CURLOPT_PORT instead, too.
DanMan
If you're testing directly ON the server, then the IP should be 127.0.0.1
DanMan
Hmm, that might be it @DanMan. I'll give it a try, thanks :)
TaraWalsh
Hmm, still no luck with that one. Will keep trying anyway
TaraWalsh
In your last edit, you're still using CURLOPT instead of CURLOPT_URL.
DanMan
A: 

Have a look at http_build_query

troelskn