【问题标题】:My Webservices not working PHP/MYSQL/JSON我的 Web 服务不工作 PHP/MYSQL/JSON
【发布时间】:2015-10-20 14:33:30
【问题描述】:

我创建了 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 无法通话,请尽快帮助我?

【问题讨论】:

  • 你很容易受到sql injection attacks 的攻击,而且由于你的“服务”脚本需要一个 GET 请求,你为什么要在 curl 中进行 POST?
  • 因为您使用 https 作为方案,我认为您需要在 curl 请求中添加更多选项 - 例如 CURLOPT_SSL_VERIFYHOST、CURLOPT_CAINFO、CURLOPT_SSL_VERIFYPEER。
  • 您的费率很低。对 SO 很重要,您必须使用已发布答案左侧、投票下方的勾号来标记已接受的答案。这将提高您的费率。通过访问此链接了解其工作原理:meta.stackoverflow.com/questions/5234/…

标签: php mysql json web-services api


【解决方案1】:

您的代码非常混乱 - 您在某些地方需要 $_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 注入是开放的。

【讨论】:

  • 非常感谢。它现在工作。在我最初的项目中,我将处理 SQL INJUNCTION。
猜你喜欢
  • 1970-01-01
  • 2017-03-04
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多