0

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.

109221793
  • 16,477
  • 38
  • 108
  • 160
  • 2
    'email' => urlencode($email), 'contact2' => urlencode($contact2), 'telephone2' => urlencode($telephone2) 'email2' => urlencode($email2); 'package' => urlencode($package) you should really fix this array! it's missing commas, there's a semicolon.. it's all messed up – sathia Sep 27 '10 at 10:07
  • @sathia, thanks, I fixed that. Embarrassed I missed that! The die("Test: ".$fields_string); is now outputting:: Test: companyName=xxx&mainContact=xxx+xxx&telephone1=xxxx with all the correct values. however that is all it outputs - It doesn't update my database and it doesn't proceed with the following mail() function or IF statement. – 109221793 Sep 27 '10 at 10:28
  • of course, now you need to comment that die() because die does what die says: Kill the execution of the program. just comment it //die("Test.. and it will go on – sathia Sep 27 '10 at 10:36
  • That would make sense!! I did that, It runs through my script fine, even sends me a confirmation email. Still doesn't update my database though :-S I'm assuming this is more of a problem with the URL of the web service rather than my code at this stage now. What do you think? – 109221793 Sep 27 '10 at 10:46

4 Answers4

3
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?

Lotus Notes
  • 6,302
  • 7
  • 32
  • 47
sathia
  • 2,192
  • 2
  • 24
  • 42
  • I tried inserting the dollar symbol where it was missing and still no joy. Thanks anyway. – 109221793 Sep 27 '10 at 09:00
  • have you added both dollars sign? what die($fields_string); prints? – sathia Sep 27 '10 at 09:07
  • @sathia, yup, both dollars sign. I didn't try die($fields_string); prints, where should I insert this? – 109221793 Sep 27 '10 at 09:11
  • foreach($curl_post_data as $key=>$value) {$fields_string .=$key. '=' .value.'&'; } rtrim($fields_string, '&'); die($fields_string); – sathia Sep 27 '10 at 09:19
  • 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? – 109221793 Sep 27 '10 at 09:27
  • 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 Sep 27 '10 at 09:45
  • @sathia...there was still no output from this. – 109221793 Sep 27 '10 at 09:53
  • 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. – 109221793 Sep 27 '10 at 10:07
1

It would be best if you can provide your PHP version.

As of PHP 5, some handy functions are bundled in the core instead of separate PECL libraries.

// If you are working with normal HTTP requests, simply do this.
$curl_post_data = http_build_query($curl_post_data);

curl_setopt($ch, CURLOPT, $url);

// This is a boolean option, although passing non-zero integer 
// will be type-casted to TRUE, count() is not the proper way.
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);

// If you really want the next statement be meaningful, do this. 
// Otherwise your HTTP response will be passed directly into 
// the output buffer.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($ch);

Keep in mind that CURLOPT_POSTFIELDS in PHP supports file uploads, by adding a '@' character followed by a full file path as the value.

You don't want to call http_build_query() on such situations.

Sample code for file upload

$curl_post_data = array('file1' => '@/home/user/files_to_be_uploaded');

While you can optionally specify MIME type, see the documentation for more information.

As said, check your PHP version first. This feature only works in PHP 5, AFAIK there are companies still hosting PHP 4.x in their servers.

Vicary
  • 1,026
  • 1
  • 15
  • 35
1

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: PHP + curl, HTTP POST sample code?

Community
  • 1
  • 1
DanMan
  • 11,323
  • 4
  • 40
  • 61
  • @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? – 109221793 Sep 27 '10 at 09:06
  • I suppose it shoud work, but you can try setting CURLOPT_PORT instead, too. – DanMan Sep 27 '10 at 09:11
  • If you're testing directly ON the server, then the IP should be 127.0.0.1 – DanMan Sep 27 '10 at 09:18
  • Hmm, still no luck with that one. Will keep trying anyway – 109221793 Sep 27 '10 at 09:27
  • In your last edit, you're still using CURLOPT instead of CURLOPT_URL. – DanMan Sep 27 '10 at 17:45
0

Have a look at http_build_query

troelskn
  • 115,121
  • 27
  • 131
  • 155