首页 文章

在PHP中使用cURL的RAW POST

提问于
浏览
103

如何使用cURL在PHP中执行RAW POST?

没有任何编码的原始帖子,我的数据存储在一个字符串中 . 数据应格式如下:

... 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 发送原始数据吗?

1 回答

  • 189

    我刚刚找到了解决方案,有点回答我自己的问题,万一其他人偶然发现它 .

    $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);
    

相关问题