简体   繁体   English

带有URL参数的PHP echo语句

[英]PHP echo statement with URL parameter

I wrote a mini script in PHP to send a POST request to the web server: 我用PHP编写了一个迷你脚本,将POST请求发送到Web服务器:

<?php

$cid = file_get_contents('cid');

function httpPost($url)
{
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POST, true);

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;
}

echo httpPost("http://172.17.0.1:2375/containers/$cid/stop?t=5");

?>

Yes, this is Docker. 是的,这是Docker。 I'm using the remote API in Docker , and this little piece of script works! 在Docker中使用了远程API ,这个小脚本就可以了! However, the ?t=5 at the end of the URL, gets ignored. 但是,URL末尾的?t = 5会被忽略。 I guess it has to do with the ? 我想这与 .

How do I get this URL properly formatted, so that the ?t=5 works correctly? 如何正确设置此URL的格式,以便?t = 5正常工作?

(I tried 1,001 ways so far, with quotes and double quotes, with no luck. After more than 4 hours spent on this, I thought stackoverflow could help?) (到目前为止,我已经尝试了1,001种方法,使用双引号和双引号,但是没有运气。在花费了4多个小时之后,我认为stackoverflow会有所帮助吗?)

Thanks... 谢谢...

NOTE: the "cid" is only a file on the hard-drive, that stores the container id. 注意:“ cid”只是硬盘驱动器上的一个文件,用于存储容器ID。 So I'm retrieving the container id from the file, and passing it to the URL (this part works, anyway). 因此,我正在从文件中检索容器ID,并将其传递给URL(无论如何,此部分都有效)。 The complete URL is written by me, ie not parsed. 完整的URL由我编写,即未解析。

Since there are no special requirements on your URL, why use an incomplete cURL wrapper function? 由于您的网址没有特殊要求,为什么要使用不完整的cURL包装函数? You can simply do 你可以简单地做

echo file_get_contents("http://172.17.0.1:2375/containers/$cid/stop?t=5");

To answer your actual question as to why your query string gets ignored, it is because it is not being sent to the server properly. 要回答有关为什么查询字符串被忽略的实际问题,这是因为未将其正确发送到服务器。 Google CURLOPT_POSTFIELDS Google CURLOPT_POSTFIELDS

Edit Since it was mentioned that the request method has to be POST, you can change things a little in your cURL code to cater for that 编辑由于提到了请求方法必须是POST,因此您可以在cURL代码中进行一些更改以适应该要求

curl_setopt($ch, CURLOPT_POSTFIELDS,"t=5");

Then you can call your function like 然后您可以像这样调用函数

echo httpPost("http://172.17.0.1:2375/containers/$cid/stop");

You might try executing like this? 您可以尝试这样执行吗?

<?php
$cid = file_get_contents('cid');

function containeraction($cid, $action, $s) {
    //Time in Seconds
    $timedelay="t=".$s;
    //Docker Container Host
    $dockerhost="172.17.0.1";
    //Host Port
    $port="2375";
    $url = "http://".$dockerhost.":".$port."/containers/".$cid."/".$action;
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $timedelay);
    $output=curl_exec($ch);
    curl_close($ch);
    return $output;
}

//containeraction(container id, action, delay)
echo containeraction($cid, "stop", "5");

?>

Since you are trying a POST request you can modify your function a bit. 由于您正在尝试POST请求,因此可以稍微修改一下功能。 For $data you can pass array("t"=>5). 对于$ data,您可以传递array(“ t” => 5)。

function httpPost($url, $data = '')
{
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POST, true);
    if ($data != '')
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;
}

Your curl setup works against Docker and passes the query string. 您的curl设置适用于Docker并传递查询字符串。 You do need to trim whitespace when reading files in case there is a new line at the end. 在读取文件时,您确实需要修剪空格,以防末尾有新行。

<?php

$cid = trim(file_get_contents('cid'));
echo "$cid\n";

function httpPost($url)
{
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POST, true);

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;
}

$url = "http://172.17.0.1:2375/containers/$cid/stop?t=6";
echo "$url\n";
echo httpPost($url)

?>

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

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