在PHP中使用cURL的原始POST
如何使用cURL在PHP中创建原始帖子 未经任何编码的原始帖子,我的数据存储在字符串中。数据的格式应如下所示:在PHP中使用cURL的原始POST,php,post,curl,put,Php,Post,Curl,Put,如何使用cURL在PHP中创建原始帖子 未经任何编码的原始帖子,我的数据存储在字符串中。数据的格式应如下所示: ... usual HTTP header ... Content-Length: 1039 Content-Type: text/plain 89c5fdataasdhf kajshfd akjshfksa hfdkjsa falkjshfsa ajshd fkjsahfd lkjsahflksahfdlkashfhsadkjfsalhfd ajshdfhsafiahfiuwhfl
... usual HTTP header ...
Content-Length: 1039
Content-Type: text/plain
89c5fdataasdhf kajshfd akjshfksa hfdkjsa falkjshfsa
ajshd fkjsahfd lkjsahflksahfdlkashfhsadkjfsalhfd
ajshdfhsafiahfiuwhflsf this is just data from a string
more data kjahfdhsakjfhsalkjfdhalksfd
一种选择是手动写入要发送的整个HTTP头,但这似乎不太理想
不管怎样,我是否可以将选项传递给curl_setopt(),即使用POST,使用text/plain,并从
$variable
发送原始数据?我刚刚找到了解决方案,类似于回答我自己的问题,以防其他人发现它
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result=curl_exec ($ch);
Guzzle库的实现:
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$httpClient = new Client();
$response = $httpClient->post(
'https://postman-echo.com/post',
[
RequestOptions::BODY => 'POST raw request content',
RequestOptions::HEADERS => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
]
);
echo(
$response->getBody()->getContents()
);
PHP CURL扩展:
$curlHandler = curl_init();
curl_setopt_array($curlHandler, [
CURLOPT_URL => 'https://postman-echo.com/post',
CURLOPT_RETURNTRANSFER => true,
/**
* Specify POST method
*/
CURLOPT_POST => true,
/**
* Specify request content
*/
CURLOPT_POSTFIELDS => 'POST raw request content',
]);
$response = curl_exec($curlHandler);
curl_close($curlHandler);
echo($response);
php会为您设置内容长度标题吗?还是您也应该设置?我根本无法实现这一点。我有一个页面,我正试图发布原始数据。该页将接收到的所有原始数据记录到数据库表中。根本没有新行。你知道自09年以来这里有什么变化吗?这对我来说很有效,没有指定任何HTTP头。我刚刚意识到这里的body可以包含任何有效的json字符串。这篇原始文章有2G的限制。如果您尝试发送大于2G的文件,它们将被截断回2G。这是对正在加载的字符串类型的限制。