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
https://forums.phpfreaks.com/topic/214603-help-with-php-curl-request/
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.

 

 

 

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?

Did you typo or is this your actual code?

 

curl_setopt ($ch, CURLOPT, $url);

 

Should be:

 

curl_setopt ($ch, CURLOPT_URL, $url);

 

Thank you very much. This was obviously my mistake. I got a HTTP 400 error with this, but at least it's an error to actually go on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.