gpt4 book ai didi

php - 我的 Web 服务不工作 PHP/MYSQL/JSON

转载 作者:行者123 更新时间:2023-11-28 23:55:53 27 4
gpt4 key购买 nike

我已经创建了 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 注入(inject)是开放的。

关于php - 我的 Web 服务不工作 PHP/MYSQL/JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31703373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com