简体   繁体   中英

My Webservices not working PHP/MYSQL/JSON

I have created web services but it is not working using cURL. When I am using file_get_contents($request) it is working.

I want to call it using cURL

Server Input Code

<?php
require_once('config.php');
$posts = array();
/* require the user as the parameter */
if(isset($_GET['roll']) and isset($_GET['name']))
{
    /* soak in the passed variable or set our own */
    $roll = $_GET['roll']; //no default
    $name = $_GET['name']; //no default
    /* grab the posts from the db */
    if($roll!="" and $name!="")
    {
        $query = "INSERT INTO STUDENT VALUES('$roll','$name')";
        if(mysql_query($query,$dblink))
        {
            $posts[] = array('status'=>'Data Inserted');
        }
        else
        {
            $posts[] = array('status'=>'Not Inserted');
        }
    }
    else
    {
        $posts[] = array('status'=>'Null Value sent');
    }
    /* disconnect from the db */
    @mysql_close($db);
}
else
{
    $posts[] = array('status'=>'Please check the arguments');
}
/* output in necessary format */
    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));
?>

Server Output Code is

<?php
require_once('config.php');
/* require the user as the parameter */
$posts = array();
if(isset($_REQUEST['roll']))
{
    $posts = array();
    /* soak in the passed variable or set our own */

    $roll = $_REQUEST['roll']; //no default

    /* grab the posts from the db */
    if($roll!="")
    {
        if($roll=="All")
        {
            $query = "SELECT * FROM STUDENT";
        }
        else 
        {
            $query = "SELECT * FROM  `STUDENT` WHERE roll =  '$roll'";
        }
        $result = mysql_query($query,$dblink) or die('Errant query:  '.$query);
        if(!$result)
        {
            $posts[] = array('status'=>'Roll Not Found');
        }
        else
        {
            /* create one master array of the records */    
            if(mysql_num_rows($result)) 
            {
            while($post = mysql_fetch_assoc($result)) 
            {
                $posts[] = array('student'=>$post);
            }
        }
        else
        {
            $posts[] = array('status'=>'Roll not Found');
        }
    }
}
else
{
    $posts[] = array('status'=>'Roll Should Not be Null');
}

/* disconnect from the db */
@mysql_close($db);
}
   else
   {
        $posts[] = array('status'=>'Please send Valid Argument');
   }
       /* output in necessary format */
        header('Content-type: application/json');
       echo json_encode(array('posts'=>$posts));
?>

Call Using file_get_contents($request) Its working

<?php
     $request="https://returns.jabong.com/tracking/WebServices/server_output.php?roll=12MCA02";
     //$request="https://returns.jabong.com/tracking/WebServices/server_input.php?roll=12MCA02&name=Aneesh Khan";
    $response = file_get_contents($request);
    print_r($response);
?>

Call using cURL - Its not working

    $sUrl = "https://returns.jabong.com/tracking/WebServices/server_input.php";
    $sData = 'roll=12MCA05&name=Aneesh A Khan';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sUrl);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $vRes = curl_exec($ch);
    curl_close($ch);

    header('Content-Type: text/json');
    echo $vRes;
?>

Call is not working using cURL , Please help me soon ?

Your code was quite mixed up - you are requiring $_GET parameters in some places, $_REQUEST parameters in others and the curl function looks like it's attempting to send via POST - but there are options missing to force curl to send via POST. Using a simple GET request via curl with the parameters appended to the url the data gets inserted.

<?php

    $sUrl = "https://returns.jabong.com/tracking/WebServices/server_input.php";
    $sData = 'roll='.uniqid().'&name='.uniqid('user_',true);

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $sUrl . '?' . $sData );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );  

    $vRes = curl_exec($ch);
    curl_close($ch);
    echo $vRes;

?>

Which method is it you want to use? And, as @Marc pointed out this code is open to sql injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM