Jump to content

Help with PHP cURL request


johnnybenimble

Recommended Posts

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?

 

I have also tried it like this with the port number,

$url = 'http://127.0.0.1/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_PORT , 85);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);

Link to comment
Share on other sites

These are suppossed to be form elements POST'ed to a script right?  Why are you naming all the elements using '$somename'?

 

I would think you would just have:

 

'companyName' =>$companyName, .. etc

 

Also the actual setting of the $companyName variable is not in evidence anywhere in the code you  provided.

 

 

 

Link to comment
Share on other sites

These are suppossed to be form elements POST'ed to a script right?  Why are you naming all the elements using '$somename'?

 

I would think you would just have:

 

'companyName' =>$companyName, .. etc

 

Also the actual setting of the $companyName variable is not in evidence anywhere in the code you  provided.

 

Apologies, it was further up in the code, I just wanted to post a snippet.

 

$companyName = $_POST['COMPNAME'];
$mainContact = $_POST['MAINCONTACT'];
$telephone1 = $_POST['TELEPHONE'];
$email = $_POST['EMAIL'];
$contact2 = $_POST['SECONDCONTACT'];
$telephone2 = $_POST['TELEPHONE2'];
$email2 = $_POST['EMAIL2'];
$package = $_POST['PACKAGE'];

 

The reason for the dollar symbols - the creator of the web service is looking for these.

 

I have tested the output with

die("Test: ".$fields_string);

and it shows the correct output that I require so I'm thinking it's not a problem with the code?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.