可以在PHP中使用cURL向HTTP请求体添加数据吗?

可以在PHP中使用cURL向HTTP请求体添加数据吗?,php,http,curl,Php,Http,Curl,我想在PHP中使用cURL向http请求体添加一些数据 这可能吗?怎么做 我正在向远程服务器发送HTTP请求。我已经添加了所有我想要的头,但是现在我需要向HTTP主体添加更多的数据,但是我不知道如何做 应该是这样的: Host: stackoverflow.com Authorization: Basic asd3sd6878sdf6svg87fS User-Agent: My user agent ... other headers... I want to add data to th

我想在PHP中使用cURL向http请求体添加一些数据

这可能吗?怎么做

我正在向远程服务器发送HTTP请求。我已经添加了所有我想要的头,但是现在我需要向HTTP主体添加更多的数据,但是我不知道如何做

应该是这样的:

Host: stackoverflow.com Authorization: Basic asd3sd6878sdf6svg87fS User-Agent: My user agent ... other headers... I want to add data to the request here
$ch = curl_init("http://stackoverflow.com/questions/1361169/possible-to-add-data-to-the-body-of-a-http-request-using-curl-in-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);

$output = str_replace('Possible to add data to the body of a HTTP request using cURL in PHP?', 'I have just changed the title of your post...', $output);

echo $output;
主机:stackoverflow.com 授权:基本asd3sd6878sdf6svg87fS 用户代理:我的用户代理 ... 其他标题。。。 我想在这里向请求添加数据 你会想看得更具体一些


如果您能更准确地了解您的需求,我可以为您提供一个示例,尽管手册页面上的示例也很好地帮助您入门。

您只能在使用CURLOPT_POSTFIELDS选项发送post请求时添加post值。

100%确定您的意思

如果您想抓取一个页面,并替换内容/向其中插入一些内容,您可以执行以下操作:

Host: stackoverflow.com Authorization: Basic asd3sd6878sdf6svg87fS User-Agent: My user agent ... other headers... I want to add data to the request here
$ch = curl_init("http://stackoverflow.com/questions/1361169/possible-to-add-data-to-the-body-of-a-http-request-using-curl-in-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);

$output = str_replace('Possible to add data to the body of a HTTP request using cURL in PHP?', 'I have just changed the title of your post...', $output);

echo $output;
这将打印此页

编辑:

添加了新信息后,我认为您应该能够使用POSTFIELDS。。记住把POST设置为1

例如(类似这样的东西-未测试)


什么类型的数据?额外标题?curl是一个完整的库,当然可以对数据执行任何操作,因为它支持协议。@AlberT想发布答案吗?:)@泽德,已经投票支持戈德瓦的答案,不需要再重复一次好的答案:)@AlberT,我明白这是怎么回事了。。。他给出了一个完全错误的答案,我给出了一个正确的答案。你给他投票,给我加了一些无效的评论。然后他看了看我的答案,意识到这是正确的答案,将它复制到他的答案中,然后被接受。最重要的是,现在你对我答案的评论对他的答案也是有效的,尽管我在任何地方都没有看到。@Zed,我没有复制你的答案。。我已经使用php和curl很长时间了。我确实看到你的答案是正确的,这是我更新答案时没有看到的+1那么,添加到http请求的数据在哪里?