简体   繁体   English

卷曲不适用于Content-Type

[英]Curl not working with Content-Type

I have following piece of code: 我有以下代码:

$input = http_build_query(array('text' => 'hello'));
$url = "http://localhost/deskline/server.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Content-Length:'.strlen($input)));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);

echo $data;

And in the server.php file this code: 在server.php文件中,此代码为:

echo $_POST['text'];

Whenever I load the php file which should echo out the text (hello), nothing happens unless I remove the 'Content-Type: text/plain' from the header. 每当我加载应该回显文本(您好)的php文件时,除非我从标题中删除“ Content-Type:text / plain”,否则什么都不会发生。 I tried using 'text/html' insted but it didn't help at all. 我尝试使用插入的“ text / html”,但这根本没有帮助。

I'll be very grateful for every piece of advice. 我将非常感谢您的每条建议。

Thank you 谢谢

I think you need to add an additional curl_setopt statement to make this a POST request. 我认为您需要添加一条额外的curl_setopt语句以使其成为POST请求。

Add the following: 添加以下内容:

curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ ch,CURLOPT_POST,1);

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

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