簡體   English   中英

我的Web服務無法正常工作PHP / MYSQL / JSON

[英]My Webservices not working PHP/MYSQL/JSON

我已經創建了Web服務,但是無法使用cURL正常工作。 當我使用file_get_contents($ request)時,它正在工作。

我想用cURL來稱呼它

服務器輸入代碼

<?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));
?>

服務器輸出代碼為

<?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));
?>

使用file_get_contents($ request)進行調用

<?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);
?>

使用cURL呼叫-無法運作

    $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;
?>

呼叫無法使用cURL進行,請盡快幫助我?

您的代碼非常混亂-您在某些地方需要$ _GET參數,在其他地方需要$ _REQUEST參數,而curl函數看起來像是嘗試通過POST發送-但是缺少一些選項來強制curl通過POST發送。 通過curl使用簡單的GET請求,並在URL后面附加參數,即可插入數據。

<?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;

?>

您要使用哪種方法? 而且,正如@Marc指出的那樣,此代碼可用於sql注入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM