[英]Curl not working with Content-Type
我有以下代码:
$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;
在server.php文件中,此代码为:
echo $_POST['text'];
每当我加载应该回显文本(您好)的php文件时,除非我从标题中删除“ Content-Type:text / plain”,否则什么都不会发生。 我尝试使用插入的“ text / html”,但这根本没有帮助。
我将非常感谢您的每条建议。
谢谢
我认为您需要添加一条额外的curl_setopt语句以使其成为POST请求。
添加以下内容:
curl_setopt($ ch,CURLOPT_POST,1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.