Buongiorno, ho una pagina1 php 7.0 che invia una richiesta per ricevere un JSON da una pagina2 php sul server che a sua volta legge dal database. La pagina1 che invia la richiesta deve inviare anche un post con la query. Le pagine sono caricate su un server Windows con php 7.2. Ho provato sia con "file_get_contents" che con "curl", entrambe le soluzioni leggono correttamente il json ma la pagina2 sul server non riceve il post con la query.
codice:
function queryws()
{
$postdata = http_build_query(
array(
'var1' => 'some content1',
'var2' => 'some content2'
)
);
$opts = array('http' =>
array(
'follow_location' => 1,
'ignore_errors' => TRUE,
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded",
// testato anche 'Content-Type: application/json'
'content' => $postdata
)
);
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
echo $json;echo "<br>";
codice pagina1 che invia la richiesta con "file_get_contents" :
codice:
function queryws()
{
$query= "TEST QUERY POST";
$query_post= "{\"message\":\"" . $query . "\"}";
$message=array( 'message'=>$query_post);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch,CURLOPT_POST, 1); //0 for a get request
curl_setopt($ch,CURLOPT_POSTFIELDS,$message);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$json = curl_exec($ch);
curl_close ($ch);
echo $json;echo "<br>";
codice pagina1 che invia la richiesta con "curl" :
codice:
$post1= "test POST";
$post2=$_GET['message'];
$myObj->post1 = "--p.1 :".$post1;
$myObj->post2 = "--p.2 :".$post2;
$myJSON = json_encode($myObj);
echo $myJSON;
codice pagina2 sul server :
json :{"post1":"--p.1 :test POST","post2":"--p.2 :"}
Grazie a tutti.