2016-06-10 96 views
1

我在PHP写了一个小脚本发送POST请求的Web服务器:PHP echo语句

<?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"); 

?> 

是的,这是码头工人。我正在使用remote API in Docker,这个小小的脚本很有用! 但是,URL末尾的?t = 5会被忽略。 我猜这跟有关?

如何正确格式化此URL,以使t = 5正常工作?

(我试过1001点的方式,到目前为止,用引号和双引号,没有运气。花在这4个多小时后,我以为计算器可以帮助?)

谢谢...

注:“cid”只是硬盘上的一个文件,用于存储容器ID。所以我从文件中检索容器ID,并将它传递给URL(无论如何,这部分工作)。 完整的网址由我编写,即未解析。

+0

您试过了吗? '$ ch = curl_init($ url);'我不确定,但这可能会改变事情。 –

+0

对Docker一无所知,所以这可能不适用于你的情况,但是如果使用mod_rewrite解析URL,则需要在.htaccess中添加选项[QSA]以传递get参数 – mikesp

+0

“stop”脚本是否期望't'参数在'GET'或'POST'数据中?把它放在'?'数据中。 – Barmar

回答

3

由于您的网址没有特殊要求,为什么要使用不完整的cURL包装函数?你可以简单地做

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

为了回答您的实际问题,为什么您的查询字符串被忽略了,那是因为它没有被发送到服务器正常。谷歌CURLOPT_POSTFIELDS

编辑既然有人提到,请求方法必须是POST,你可以改变的事情一点点在你的卷曲的代码,以满足该

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

然后,你可以打电话给你功能如

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

集装箱站是一个POST端点https://docs.docker.com/engine/reference/api/docker_remote_api_v1.19/#stop-a-container – Matt

1

你可能会尝试像这样执行?

<?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"); 

?> 
+0

嗯感谢这一点,但URL参数仍然被忽略。 – Tux

+0

哦,我的道歉我会在几分钟内回复给你 –

+0

除了使用连接而不是'$ cid'变量插值之外,你有什么改变?为什么会有所作为? – Barmar

1

由于您正在尝试POST请求,您可以稍微修改您的功能。对于$ data,你可以传递数组(“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; 
} 
+0

你介意完成你的一段代码,以便我可以看到你如何构造结果网址? (我真的是PHP中的noob) – Tux

+1

他的意思是,echo httpPost(“http:// 172.17.0.1:2375/containers/$ cid/stop”,array(“t”=> 5)); - 我必须在斜线之前/之后放一些空格,因为它不断打破完整的语法。 –

+0

'CURLOPT_POSTFIELDS'如何修改查询字符串? – Matt

0

您的卷曲设置对Docker起作用并传递查询字符串。在读取文件时,如果最后有新行,则需要修剪空白。

<?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) 

?> 
+0

嗨,我怎样才能传递参数没有“?” 例如$ URL =“http://172.17.0.1:2375/containers/$cid/stop和 $ PARAM =‘6’,我们可以在接收脚本收到6? 这里是我的问题,如果你能帮助我https://stackoverflow.com/questions/39930240/php-curl-post-value-without-querystring-parameter – p4pravin