Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jaahvicky's avatar

How to send data to a SOAP Web Service using Laravel & PHP

I trying to send an XML vacancies to a Web Service using Laravel and I'm struggling to figure out how to connect to the web service, authorise and send the required data.

I have tried to use curl, but I am getting Error: "" - Code: 0.

Below is my Code

$result // IS MY XML file

$username = 'username
$password = 'password
$URL = 'http://xxxxxx.com;

//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"xmlRequest=" . $result);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_exec($ch);

if(!curl_exec($ch)){
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

curl_close($ch);

}

Another question that would be of help would be - Is there another way of sending data to a SOAP Web Service without having to use Curl?

Thank you in advance

0 likes
4 replies
jekinney's avatar

SOAP is kinda old way of doing APIs and to my knowledge isn't being updated or made easier with tools like Json.

It's been awhile but I think your pretty much stuck as is meaning curl calls.

jaahvicky's avatar

@jekinney I agree with what you are saying. Unfortunately the only way I can send data is by using SOAP to this Web Service.

jekinney's avatar

I hear yeah. Lot of services use soap still mainly for internal use. To much time and money to set it up to change and is still useable.

Please or to participate in this conversation.