简体   繁体   English

PHP文件在本地工作,但不能在服务器上工作

[英]PHP file works locally, but not on server

I'm currently developing a mobile app on windows phone which requires me to send a request to a PHP file located on a server. 我目前正在开发Windows Phone上的移动应用程序,这需要我向服务器上的PHP文件发送请求。

I'm running into an issue where the request throws an error when on the server, but doesn't when I change the code to page the local PHP file. 我遇到了一个问题,其中在服务器上时请求会引发错误,但在更改代码以分页本地PHP文件时却没有。 Any ideas why? 有什么想法吗?

Here's my request code: 这是我的请求代码:

     string sendString = "";
        sendString += "&lat=" + (App.Current as App).mapCenter.Latitude;
        sendString += "&lng=" + (App.Current as App).mapCenter.Longitude;
        sendString += "&address=" + (App.Current as App).locationAddress;
        if (checkNumber(CountryCode.Text, AreaCode.Text, Number.Text))
        {
            sendString += "&phone=" + CountryCode.Text + "" + AreaCode.Text + "" + Number.Text;
        }
        else
        {
            MessageBox.Show("Phone number invalid");
        }
        if (checkEmail(email.Text))
        {
            sendString += "&email=" + email.Text;
        }
        else
        {
            MessageBox.Show("Invalid Email");
        }
        sendString += "device=WindowsPhone";
        sendString += (bool)prefsms.IsChecked ? "&contactMethod=sms" : "&contactMethod=email";
        ListPickerItem selectedItem = (ListPickerItem)ServiceType.SelectedItem; //Cast listpickeritem into String
        string content = (string)selectedItem.Content;
        sendString += "&serviceType=" + content;
        sendString += "&version=2.0";
        sendString += "&language=" + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
        (App.Current as App).watcher.Stop();
        sendData = sendString;


        if ((checkNumber(CountryCode.Text, AreaCode.Text, Number.Text)) && (checkEmail(email.Text)))
        {
            //Send data to server 
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.BeginGetRequestStream(new AsyncCallback(RequestCallBack), myRequest);
        }
        else
        {
        }

private void RequestCallBack(IAsyncResult asyncresult)
{
        HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest;
        Stream dataStream = myRequest.EndGetRequestStream(asyncresult);
        //Avoid trashing procedure
        this.Dispatcher.BeginInvoke(delegate()
        {
            StreamWriter writer = new StreamWriter(dataStream);
            writer.Write(sendData);
            writer.Flush();
            writer.Close();
            //Expect a response
            myRequest.BeginGetResponse(new AsyncCallback(ResponseCallBack), myRequest);
        });

    }
  private void ResponseCallBack(IAsyncResult asyncresult)
    {
        HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest;
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncresult);
        // Avoid trashing the procedure
        this.Dispatcher.BeginInvoke(delegate()
        {
            // Retrieve response stream
            Stream responseStream = myResponse.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            // Show response
            responseTxt = reader.ReadToEnd();
            MessageBox.Show("" + responseTxt);
        });

    }

I'm sending a string to the server, and the server's PHP code looks like this (the test php is exactly the same): 我正在向服务器发送一个字符串,服务器的PHP代码如下所示(测试php完全相同):

require_once("includes/globals.php");

$vars = ($PRODUCTION)?$_POST:$_REQUEST;

$MINIMUM_VERSION = 2.0;
function validServiceType($type)
{
return $type == "taxi" || $type == "ladies" || $type == "limo" || $type ==   "handicap";
}

if(!isset($vars['version']) || ((float)$vars['version']) < $MINIMUM_VERSION)
{
echo "oldVersion";
}
elseif(isset($vars['lat'], $vars['lng'], $vars['phone'], $vars['language']))
{
     /*do stuff */
if($data['contactMethod'] == "email" && $data['email'] == "")
{
    echo "invalid";
    die();
}
if(DB_AddHail($data))
    echo "processing";
else if (($company = DB_GetReferralTaxi($data['lat'], $data['lng'], $data['serviceType'])))
{
    /*do some more stuff*/
}
else
    echo "sorry";
}
else
{
echo "invalid";
}

?>

I keep getting an OldVersion error but I've set the version number to be 2.0 already...any ideas as to what's going on? 我一直收到OldVersion错误,但是我已经将版本号设置为2.0 ...关于发生了什么的任何想法?

UPDATE: I've now been able to figure the source of the problem; 更新:我现在已经能够找出问题的根源; the HttpWebRequest isn't sending any data over to the server at all; HttpWebRequest根本不向服务器发送任何数据; my $_POST is just an empty array. 我的$ _POST只是一个空数组。 Any ideas as to why? 有什么想法吗?

UPDATE 2: The error I was getting is error 302 ("my URI I was using was just http:// instead of https://). However after changing the protocol to https://, it still doesn't work. Is my code incorrect for that part? 更新2:我得到的错误是错误302(“我正在使用的URI只是http://而不是https://)。但是,在将协议更改为https://之后,它仍然不起作用。我的代码在那部分不正确吗?

Can you try, immediately before the line starting with if(!isset($vars['version']), adding: 在以if(!isset($ vars ['version'])开头的行之前,您是否可以尝试添加以下内容:

echo "version: " . $vars['version']; 
exit; 

See if it outputs anything to the browser. 查看它是否将任何内容输出到浏览器。 It might be that the version of php you're using doesn't like the $vars = ($PRODUCTION)?$_POST:$_REQUEST; 可能是您正在使用的php版本与$ vars =($ PRODUCTION)?$ _ POST:$ _ REQUEST不一样。 syntax, and you might have to manually go through and so something like 语法,您可能需要手动进行操作,例如

$vars['version']=$_POST['version']; 
$vars['somethingElse']=$_POST['somethingElse'] 

etc... 等等...

Make sure your on 5.4.3 and run your script with phpinfo(); 确保您在5.4.3上运行并使用phpinfo()运行脚本; to check. 去检查。 Try change you're PHP/C# to receive/send GET requests instead of POST requests. 尝试将您更改为PHP / C#,以接收/发送GET请求而不是POST请求。

That way you can atleast test to see when you send the php script a request from the browser for example http://yourdomain.com/script.php?name=John+Smith&Age=23 , if you're getting expected results. 这样,您可以进行至少一次测试,以查看何时从浏览器向php脚本发送请求(例如http://yourdomain.com/script.php?name=John+Smith&Age=23) ,以获取预期的结果。 Download https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/ to check how the headers are working from the browser point of view when you're checking the results. 下载https://addons.mozilla.org/zh-CN/firefox/addon/live-http-headers/,以在检查结果时从浏览器的角度检查标题的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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