John Davidson

I have a service that I need to consume and convert to json format in a html page with php

0 comments
Message:


This is my code and I am using the soap library but when executing it throws me an error HTTP Status 415 - Media type not in, how can I convert the service I am consuming to json in an html page


<?php 
require_once('lib/nusoap.php');
//Data, connection, auth
//$dataFromTheForm = $_POST['fieldName']; // request data from the form
//En esta parte es donde debería abrir la conección
$soapUrl = "http://192.168.26.16:8080/Licencias/rest/wsbepsamov?op=DoSomething"; // asmx URL of WSDL
$user = "test"; // username
$password = "123456"; // password
$documento = "34115410";
$tarjeta = "6274311750000090";
$dataFromTheForm = "2021";
// xml post structure

$url = $soapUrl;

// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$password .":".$documento.":".$tarjeta ); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);



// converting
$response = curl_exec($ch);
curl_close($ch);

// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);

// convertingc to XML
$parser = simplexml_load_string($response2);
// user $parser to get your data out of XML response and to display it.

?>


My Answer:
To convert the SOAP response to JSON format in an HTML page, you can use PHP's json_encode function. Here's an example of how you can modify your code to achieve this:

php
require_once('lib/nusoap.php');

$soapUrl = "http://192.168.26.16:8080/Licencias/rest/wsbepsamov?op=DoSomething";
$user = "test";
$password = "123456";
$documento = "34115410";
$tarjeta = "6274311750000090";
$dataFromTheForm = "2021";

// XML post structure
$xml_post_string = '







';

// PHP cURL for HTTPS connection with authentication
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'Content-Length: ' . strlen($xml_post_string)
));

$response = curl_exec($ch);
curl_close($ch);

// Convert SOAP response to JSON
$parser = simplexml_load_string($response);
$json_response = json_encode($parser);

// Display JSON response in HTML
echo "
";
print_r($json_response);
echo "
";
?>


In this code snippet, the SOAP response is converted to JSON using json_encode after parsing the XML response. The JSON response is then displayed in the HTML page using the print_r function. You can further customize the display of the JSON response in your HTML page as needed.

Rate this post

4 of 5 based on 7607 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us