【问题标题】:PHP, Curl Get Variable and ResponsePHP,Curl 获取变量和响应
【发布时间】:2013-06-18 09:17:14
【问题描述】:

我有一个表格...

<form action="response.php" method="get" name="titles">
<p><input id="titles" onclick="this.select()" type="text" name="titles" value="Live Now! DJ Name - Show Name" /> <input type="submit" value="Submit" /></p>
</form>

将变量 $titles 传递给 response.html。但是它不能正常工作。

我的response.php输出的格式应该是……

http://adminusername:adminpassword@mysite.com:8000/admin/metadata?mount=/mountname&mode=updinfo&song=$myvariableforminputhere

这里是 response.html 表单似乎可以工作,但我在包含我的变量时有问题。对不起,我有点笨拙,并且借用了其他人的代码:/

<html>
<head>
<title>PHP Form Variables Example</title>
</head>
<body>

<?php
{
//simply copy and paste this code block for each server you need to add
$serv["host"][] = "mysite.com"; //ip or hostname the server is running on. Don't include http://
$serv["port"][] = "8000"; //port the server is running on
$serv["mount"][] = "/mymount"; //this format: /mount
$serv["user"][] = "user"; //icecast server username
$serv["passwd"][] = "pass"; //icecast server password
echo $_GET["titles"];

    {
        $mysession = curl_init();
        curl_setopt($mysession, CURLOPT_URL, "http://".$serv["host"][$count].":".$serv["port"][$count]."/admin/metadata?mount=".$serv["mount"][$count]."&mode=updinfo&song=test".$titles);
        curl_setopt($mysession, CURLOPT_HEADER, false);
        curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($mysession, CURLOPT_POST, false);
        curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($mysession, CURLOPT_USERPWD, $serv["user"][$count].":".$serv["passwd"][$count]);
        curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($mysession, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        curl_setopt($mysession, CURLOPT_CONNECTTIMEOUT, 2);
        curl_exec($mysession);
        curl_close($mysession);
    }

    echo "song updated";
}
?>

<p><a href="javascript:history.go(-1)">Back</a></p>

</body>
</html> 

编辑:

感谢用户:byf-ferdy 下面的 response.php 现在可以与我上面的表单代码一起使用。还要感谢 http://forum.loudcity.net/viewtopic.php?id=4160 的 Audioprobe 提供原始代码!

<html>
<head>
<script>
//Set up java for back button
function goBack()
  {
  window.history.back()
  }
</script>
</head>
<body>

<?php

//simply copy and paste this code block for each server you need to add
$serv["host"][] = "mysite.com"; //ip or hostname the server is running on. Don't include http://
$serv["port"][] = "8000"; //port the server is running on
$serv["mount"][] = "/mymount"; //this format: /mount
$serv["user"][] = "user"; //icecast server username
$serv["passwd"][] = "pass"; //icecast server password

$encoded = urlencode($_GET['titles']); //Make sure '+' and spaces are sent correctly by encoding all +'s to be %2B

        $mysession = curl_init();
        curl_setopt($mysession, CURLOPT_URL, "http://".$serv["host"].":".$serv["port"]."/admin/metadata?mount=".$serv["mount"]."&mode=updinfo&song=".$encoded);
        curl_setopt($mysession, CURLOPT_HEADER, false);
        curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($mysession, CURLOPT_POST, false);
        curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($mysession, CURLOPT_USERPWD, $serv["user"].":".$serv["passwd"]);
        curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($mysession, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        curl_setopt($mysession, CURLOPT_CONNECTTIMEOUT, 2);
        curl_exec($mysession);
        curl_close($mysession);

    echo "Titles updated: ";
    echo $_GET["titles"];

?>
<!--Implement back button-->
<p><input type="button" value="Back" onclick="goBack()"></p>
</form>

</body>
</html> 

【问题讨论】:

  • 您是否删除了某些条件或循环?或者为什么你的代码中有{}?它通常与例如if 声明:if (condition) { }。您还可以描述“表单似乎可以工作,但是我在包含我的变量时有问题。这个?什么不起作用?您是否收到任何错误消息?如果您使用var_dump($_GET['titles'])会发生什么?而不是echo $_GET['titles']?
  • 表格似乎可以工作,但我在包含我的变量时有问题。这意味着我的 HTML 表单代码似乎可以将我的变量传递给 response.php http://www.mysite.com/filelocation/response.php?titles=myvariable。关于其他问题,我会尽快回复您。
  • 我删除了一个 if 语句。我应该删除所有大括号吗?
  • 添加 var_dump($_GET['titles']) 会导致输出... string(10) "myvariable"

标签: php variables curl get icecast


【解决方案1】:

正如我在 cmets 中已经写过的,您应该删除大括号,因为它们在您的代码中已过时。同时删除所有行

$serv["host"][] = "budgiecollective.dyndns.org";

[] 所以这条线看起来像这样:

$serv["host"] = "budgiecollective.dyndns.org"; 

然后删除代码中出现的任何[$count],因为不再使用该变量。 最后但并非最不重要的一点是将这一行中的$titles 更改为$_GET['titles']

curl_setopt($mysession, CURLOPT_URL, "http://".$serv["host"][$count].":".$serv["port"][$count]."/admin/metadata?mount=".$serv["mount"][$count]."&mode=updinfo&song=test".$titles);

【讨论】:

  • 感谢所有提示。我已经进行了更新,并认为我非常接近。也许如果我添加一个打印/写入行来显示服务器的 xml 响应,我可能能够读取错误是什么。我期望的回应是......
  • &lt;iceresponse&gt; &lt;message&gt;Metadata update successful&lt;/message&gt; &lt;return&gt;1&lt;/return&gt; &lt;/iceresponse&gt;
  • 您请求的响应正文可以通过$content = curl_exec($mysession)访问
  • $content = curl_exec($mysession); 产生错误Warning: curl_exec(): 1 is not a valid cURL handle resource
  • curl_exec 不起作用。但它的工作..谢谢!我将发布我的新代码以帮助未来的人们。最终,我想解析 xml 响应消息,为表单的用户提供一些有意义的反馈。但至少我工作!
猜你喜欢
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 2018-07-23
  • 2015-03-30
  • 2016-06-23
  • 2016-10-27
相关资源
最近更新 更多