Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP web响应_Php - Fatal编程技术网

PHP web响应

PHP web响应,php,Php,我有一个程序,它可以向php页面发出web请求,这里不涉及浏览器,我需要使用php将一些数据发送回程序。。。我该怎么做?您考虑过Web服务吗?这可能太过分了。否则发布或获取并用PHP响应 您所描述的基本上是一个网站。只需使用echo来回显您想要返回的内容 <?php echo "thing that i want to send back"; ?> post和get似乎真的是这里的解决方案。尽管我对这种技术不是很满意,因为如果URL中的参数不正确或未经验证,它很容易出错。也许

我有一个程序,它可以向php页面发出web请求,这里不涉及浏览器,我需要使用php将一些数据发送回程序。。。我该怎么做?

您考虑过Web服务吗?这可能太过分了。否则发布或获取并用PHP响应


您所描述的基本上是一个网站。

只需使用echo来回显您想要返回的内容

<?php
   echo "thing that i want to send back";
?>

post和get似乎真的是这里的解决方案。尽管我对这种技术不是很满意,因为如果URL中的参数不正确或未经验证,它很容易出错。也许考虑一下PHP以外的东西?但是答案是,它将像上面其他人所说的那样工作:

取决于您需要从PHP返回的数据类型,并且假设您同时控制C应用程序和PHP应用程序,您很可能会使用WebClient类之类的东西,它允许您对URL进行HTTP调用,您可以将其指向PHP应用程序。一旦发出请求,您将在PHP应用程序中执行任何必要的处理,然后很可能将其回显到C应用程序,这也取决于您使用的数据类型一个简单的HTTP响应由头和体组成。 标题不会显示在您的web浏览器中,而是发生在clientbrowser服务器通信web服务器中—简单地说

web服务器将返回到您的程序,也就是所谓的标题,您必须注意这些标题

使用php脚本,您可以使用echo发送输出:


你应该像其他人说的那样,回应你想要回报的东西

对于c应用程序,您应该等待响应,并读取响应流以获取回声

    public GetResponeFromPHP()
    {

        HttpWebRequest request = null;

        HttpWebResponse resp = null;

        try
        {
            request = (HttpWebRequest)WebRequest.Create("http://www.URL_of_php.php");
            request.Method = "POST";
            request.Credentials = CredentialCache.DefaultCredentials;

            // execute the http request and get the response 
            resp = request.GetResponse() as HttpWebResponse;


            if (resp.StatusCode == HttpStatusCode.OK)
            {
                //read the response stream 
                String responseText;
                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                {
                    responseText = sr.ReadToEnd();
                }

                // Now use respones text to get the echos //

            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (resp != null)
                resp.Close();
        }

    }

如果您不介意,在提出问题时,请尝试包含代码和最坏情况下的伪代码。它也会在将来的引用中帮助其他人:您的注释不会真正向代码中添加任何内容,从代码中可以明显看出,`//声明WebRequest类的HTTP特定实现`。评论应解释如何以及为什么,如果needed@diggingforfire:谢谢,你说得对,我从我的一个旧应用程序中取了一个例子,但忘了删除它们。固定的
    public GetResponeFromPHP()
    {

        HttpWebRequest request = null;

        HttpWebResponse resp = null;

        try
        {
            request = (HttpWebRequest)WebRequest.Create("http://www.URL_of_php.php");
            request.Method = "POST";
            request.Credentials = CredentialCache.DefaultCredentials;

            // execute the http request and get the response 
            resp = request.GetResponse() as HttpWebResponse;


            if (resp.StatusCode == HttpStatusCode.OK)
            {
                //read the response stream 
                String responseText;
                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                {
                    responseText = sr.ReadToEnd();
                }

                // Now use respones text to get the echos //

            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (resp != null)
                resp.Close();
        }

    }