可以使用PHP中的cURL将数据添加到HTTP请求的主体中吗?

noh*_*ohj 3 php curl http

我想在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

god*_*dva 6

不是100%肯定你的意思......

如果你想抓取一个页面,并替换内容/插入一些内容 - 你可以这样做:

$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;
Run Code Online (Sandbox Code Playgroud)

这将打印此页面...

编辑:

添加新信息后,我认为您应该可以使用POSTFIELDS ..只需记住将POST设置为1 ..

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
curl_setopt($ch, CURLOPT_USERAGENT, "My user agent"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $myOtherHeaderStringVariable); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "I want to add data to the request here");
$output = curl_exec($ch);
Run Code Online (Sandbox Code Playgroud)