gpt4 book ai didi

php - 向 phpMyAdmin 发送 NSURLRequest 会导致 NSData 成为 php 源代码

转载 作者:行者123 更新时间:2023-11-29 22:27:30 24 4
gpt4 key购买 nike

“升级”到 OS X Yosemite 10.6.6 后,我花了一个晚上试图解决问题。重新安装 MySQL/phpMyAdmin/Apache/php5 后,升级前有效的代码仍然存在问题。以下函数向本地托管的 php 文件发送 URL 请求,以从 MySQL 数据库获取 JSON 寓言格式的数据:

-(NSArray*) GetJSONArrayForURL:(NSString*)url
{
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:url];

// Create the request
//NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

NSURLResponse* response = nil;
NSData *rawdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSMutableData *downloaded = [[NSMutableData alloc] init];
[downloaded appendData:rawdata];

// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:downloaded options:NSJSONReadingAllowFragments error:&error];

return jsonArray;
}

但是,返回的原始数据是它所请求的PHP文件的源代码:

<?php
$serverName = "localhost";
$databaseName = "GUESTLISTER";
$userName = $_GET['username'];
$password = $_GET['password'];
$POST = "POST";
$GET = "GET";

// Create connection
$con=mysqli_connect($serverName,$userName,$password,$databaseName);

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Locations'
//$sql = "SELECT * FROM users";
$sql = $_GET['query'];
$action = $_GET['action'];

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
$id = mysqli_insert_id($con);
if($action == $GET)
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();

// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}

// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
else if ($action == $POST)
{
echo $id;
}
}

// Close connections
mysqli_close($con);
?>

请求是:

http://localhost/webservice.php?&username=admin&password=guestlister123&query=SELECT%20 *%20FROM%20users&action=GET

从 Web 浏览器 ping 时运行良好,但由于某些奇怪的原因,当 Xcode 请求时,源代码会作为数据返回。

请指教,瑞安

最佳答案

通常,当返回相同的源代码而不是其输出时,它表明 Php 没有被解析为 Php 的问题。这意味着您的网络服务器 - Apache 的设置存在问题,因为它无法处理 Php。造成这种情况的原因可能有很多,其中有一些可能是

  • Apache 已关闭,可能需要打开,但由于在 xcode 之外它可以工作,因此情况可能并非如此
  • PHP 的 Apache 配置未正确设置
  • 该网站不在 Apache 期望的文件夹中。 htdocs 通常是 Apache 根据 php.ini 配置查找 php 脚本的默认设置。最快的方法是运行 phpinfo() ;在 php 文件的开始和结束 php 标记之间,以确定您的设置是否一切正常。

phpinfo();

如果设置正确,您将看到一个输出页面,其中包含有关 php 和 apache 设置的所有信息,否则它只会返回 phpinfo() 行作为输出。最后,可能只是 Xcode 无法与您的 Apache 设置正常工作,因此通过浏览器直接运行脚本而不是在我从未尝试过的 Xcode 中运行脚本可能会更有好处。

关于php - 向 phpMyAdmin 发送 NSURLRequest 会导致 NSData 成为 php 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146840/

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