PHP POST method into RESTful web service

Could someone kindly help me out on this? The simple thing I want to do is to POST into a RESTful web service using PHP. I have been able to GET the values using PHP, now I want to POST into it. This is the PHP file using curl.

[php]<?php
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$url = "http://localhost:8080/WMWS/webresources/entity.userregistration";

$data = array('username' => $username, 'password' => $password);
$initializeCurl = curl_init();
curl_setopt($initializeCurl, CURLOPT_URL, $url);
curl_setopt($initializeCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($initializeCurl, CURLOPT_POST, true);
curl_setopt($initializeCurl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($initializeCurl);
curl_close($initializeCurl);

?>[/php]
I am using XAMPP and I have enabled curl in PHP.ini file. Whenever I insert values into the form input of HTML and click the submit button, it only return the PHP code on the browser and does nothing like insertion. PHP professionals, please review the code and help me with working answers on how to handle it. This is is the HTML file:

[code]

Username

Password

Register

[/code]

Your code works fine. I’ve just tested it

test.php
[php]<?php

if (!empty($_POST[‘username’]) && !empty($_POST[‘username’])) {
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$url = “http://test.local/ken4ward/curl.php”;

$data = array(‘username’ => $username, ‘password’ => $password);
$initializeCurl = curl_init();
curl_setopt($initializeCurl, CURLOPT_URL, $url);
curl_setopt($initializeCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($initializeCurl, CURLOPT_POST, true);
curl_setopt($initializeCurl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($initializeCurl);
var_dump($response); // string(37) “{“username”:“test”,“password”:“test”}”
curl_close($initializeCurl);
}

?>

Username Password Register [/php]

curl.php
[php]<?php

echo json_encode($_POST); [/php]

Works just like expected

You will have to debug the code and see if something somewhere goes wrong. Do you have reporting on for all errors/warnings/notices?

[hr]

Btw, you shouldn’t send credentials over http…

Thanks Karma. I appreciate your support. I think it’s giving a better performance than it was before. The new development is that though it has not been inserting but at least it issues an understandable error of “Unsupported Media Type” and not displaying code as it was earlier. Please, help me look into it what exactly am I to do to correct this error? I’d thought it is because I did not add a content-type, after adding it it gives the same result. I am still running both servers on localhost. The web service is created in Netbeans using Java while the calling application is created using PHP. For an overview on the error report see the attached image.


Hi JimL,

I apologize for interrupting. I have a similar problem. I am trying to submit form data to a WSDL. Please see my code below:

<?php $fname = $_POST["firstname"]; $lname = $_POST["lastname"]; $officenum = $_POST["officenumber"]; $cell = $_POST["cellnumber"]; $email = $_POST["email"]; $dropdown = $POST["contactmethod"]; $message = $_POST["subject"]; $formcontent="From: $fname \n Message: $message"; $recipient = "[email protected]"; $subject = "Contact Form"; $mailheader = "From: $email \r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); $result = ""; try { $strXML = '<?xml version="1.0" encoding="utf-8"?> '; $objWebService = new SoapClient( "https://leads.cmscloud.co.za/service.asmx?wsdl", array( "location" => "https://leads.cmscloud.co.za/service.asmx?op=SaveLead", "trace" => true, "exceptions" => true ) ); $params = array( "xml" => $strXML ); $result = $objWebService->SaveLead( $params ); } catch ( Exception $ex ) { // Check what the error is. $result = $ex->getMessage();

}
echo $result;
echo “Thank You!”. $fname."!";

?>

Start your own thread. Don’t hijack someone else’s 5 years later.

Sponsor our Newsletter | Privacy Policy | Terms of Service