Visualizzazione dei risultati da 1 a 2 su 2

Discussione: curl e xml

  1. #1

    curl e xml

    Ciao a tutti,

    ho un problema da porvi...sto testando le chiamate curl per inviare a ricevere xml, praticamente invio un piccolo xml e voglio che la mia pagina di response me lo ritorni modificato. Ho queste pagine:

    codice:
    /*------------- index.php ---------------*/
    
    <?php
    	require_once("function.php");
    	
    	$xml = new SimpleXMLElement("<say></say>");
    	$xml->addChild("hello", "hello world!");
    	
    	echo "
    
    ".htmlentities($xml->asXML())."</p>";
    	
    	echo "
    
    ".htmlentities(getResponse($xml->asXML()))."</p>";
    ?>
    codice:
    /*------------- function.php ---------------*/
    
    <?php
    	function getResponse($xml){
    		echo "
    
    Entro nella funzione</p>";
    		// ho due parametri che sono le variabili 'sistema e xml'	
    	
    		$params['sistema']=$_SERVER['SERVER_ADDR'];;
    		$params['xml']=$xml;
    		$errore="";
    	
    	
    		$url="http://127.0.0.1/varie/curl_test/response.php";
    		
    		$ch=curl_init();
    		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    		curl_setopt($ch, CURLOPT_POST,1);
    		curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    		curl_setopt($ch, CURLOPT_URL,$url);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https
    		ob_start();
    	
    		$ER_result=curl_exec($ch);
    		
    		echo "
    
    ho preso il risultato della chiamata curl</p>";
    		
    		//Nella variabile $ER_result ho il valore di ritorno della procedura che sto chiamando
    		//Verifica errore
    		if (strpos($ER_result,"00")===false)
    			$my_str="ERRORE";
    		else
    			$my_str="";
    	
    		if(curl_errno($ch))
    			$error= curl_error($ch);
    		ob_end_clean();
    		curl_close($ch);
    		
    		echo "
    
    ritorno il valore</p>";
    		return $ER_result;
    	}
    ?>
    codice:
    /*-------------- response.php -----------------*/
    
    <?php
            $aXML = simplexml_load_string(stripslashes($_POST["xml"]));
    	$newXML = new SimpleXMLElement($aXML);
    	$newXML->addChild("answer", "Hi guys!");
    	echo $newXML->asXML();
    ?>
    praticamente mi entra correttamente nella funzione fa la sua richiesta curl ma c'è un problema nel parsing xml (sicuramente è una stupidata, ma sono ancora abbastanza alle prime armi)...la pagina di output è questa:
    codice:
    <?xml version="1.0"?> <say><hello>hello world!</hello></say>
    
    Entro nella funzione
    
    ritorno il valore
    
    
     Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\Programmi\EasyPHP 3.0\www\varie\curl_test\response.php:12 Stack trace: #0 C:\Programmi\EasyPHP 3.0\www\varie\curl_test\response.php(12): SimpleXMLElement-&gt;__construct('') #1 {main} thrown in C:\Programmi\EasyPHP 3.0\www\varie\curl_test\response.php on line 12
    come vedete crea correttamente l'xml e ritorna il valore della chiamata curl...se io in response faccio semplicemente echo $_POST["xml"] funziona correttamente. Qualcuno sa dove sta l'errore?? Grazie a tutti!

  2. #2
    Ok...risolto smanettandoci tt mattina...posto la soluzione se a qualcuno può interessare / servire:

    codice:
    /*------------- index.php ------------*/
    
    <?php
    	require_once("function.php");
    	
    	$xml = new SimpleXMLElement("<say></say>");
    	$xml->addChild("hello", "hello world!");
    	
    	echo "
    
    ".htmlentities($xml->asXML())."</p>";
    	
    	$response = getResponse($xml->asXML());
    	$respXml = simplexml_load_string($response);
    	
    	echo "
    
    ".htmlentities($respXml->asXML())."</p>";
    	echo "He says: ".$respXml->hello."
    ";
    	echo "I answer: ".$respXml->answer."
    ";
    ?>

    codice:
    /*------------- function.php ------------*/
    
    <?php
    	function getResponse($xml){
    		echo "
    
    Entro nella funzione</p>";
    		// ho due parametri che sono le variabili 'sistema e xml'	
    	
    		$params['sistema']=$_SERVER['SERVER_ADDR'];;
    		$params['xml']=$xml;
    		$errore="";
    	
    	
    		$url="http://127.0.0.1/varie/curl_test/response.php";
    		
    		$ch=curl_init();
    		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    		curl_setopt($ch, CURLOPT_POST,1);
    		curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    		curl_setopt($ch, CURLOPT_URL,$url);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https
    		ob_start();
    	
    		$ER_result=curl_exec($ch);
    		
    		echo "
    
    ho preso il risultato della chiamata curl</p>";
    		
    		//Nella variabile $ER_result ho il valore di ritorno della procedura che sto chiamando
    		//Verifica errore
    		if (strpos($ER_result,"00")===false)
    			$my_str="ERRORE";
    		else
    			$my_str="";
    	
    		if(curl_errno($ch))
    			$error= curl_error($ch);
    		ob_end_clean();
    		curl_close($ch);
    		
    		echo "
    
    ritorno il valore</p>";
    		return $ER_result;
    	}
    ?>

    codice:
    /*------------- response.php ------------*/
    
    <?php
    	$aXML = simplexml_load_string(stripslashes($_POST["xml"]));
    	$aXML->addChild("answer", "Hi guys!");
    	echo $aXML->asXML();
    ?>
    il sistema prende in input un xml creato con simpleXML e ritorna una risposta aggiungendo un elemento all'xml di partenza...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.