【问题标题】:CURL Post not working (Body is not being sent)CURL Post 不起作用(未发送正文)
【发布时间】:2017-09-15 15:35:34
【问题描述】:

我有这种方法,我试图用POST 发送POST 请求正文:

public function executePost($url, $theBody, $headers){

    $data_string = '{"apple": "fruit", "turnip": "vegetable"}'; // hard coded for now                                                                               
    \Log::info($data_string);                                                                                               
    $ch = curl_init($url);                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    ));                                                                                                                   

    $result = curl_exec($ch);

    \Log::info($result);
}

在接收端我没有得到我做的数据:

\Log::info(\Input::all());

我什么也没得到,即一个空数组。我似乎无法弄清楚这有什么问题。

它可以在另一台使用 WAMP 的计算机上运行,​​我使用的是 Ubuntu 16.04 和 PHP 5.6。

【问题讨论】:

  • 能否告诉我们接收端代码?
  • 尝试在接收端获取数据 $_POST = json_decode(file_get_contents("php://input"), true );和 print_r($_POST);它和 var_dump($result);
  • 试过了,不行。
  • 所以你首先要发送的$data_string请检查一下,这是不是一个有效的jsonjsonformatter.curiousconcept.com

标签: php json laravel curl


【解决方案1】:

尽量让数据看起来像这样:

$post = [
   'username' => 'user1',
   'password' => 'passuser1',
   'gender'   => 1,
];

【讨论】:

    【解决方案2】:

    请尝试这样通过

    $data_string = '{"apple": "fruit", "turnip": "vegetable"}';                                                                                             
    $ch = curl_init('http://requestb.in/13zghda1');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $data_string));                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                                                           
    $result = curl_exec($ch);
    

    请查看https://requestb.in/13zghda1?inspect 上的回复 在后端,您现在将获得数据

    【讨论】:

    • 我需要使用 curl 请求发送 json 输入。并且 curl 帖子字段需要一个字符串而不是 php 数组。
    • 你需要使用 json 的 header。首先将数组转换为json $data_string = json_encode($data); 之后,您可以设置json的设置` curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string ))); `
    • 我正在这样做,请查看整个方法。
    • 你需要使用php输入法读取你后端的内容` $jsonStr = file_get_contents("php://input"); //读取HTTP正文。 $json = json_decode($jsonStr); ` 我使用了测试API Url & 它似乎收到了原始数据requestb.in/13zghda1?inspect
    • 试过了..还是空了..问题出在发送端..接收器工作正常。
    【解决方案3】:

    如果你想发布一个数组($data),试试这个:

    $data = ['apple' => 'fruit', 'turnip' => 'vegetable'];
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    

    【讨论】:

    • @MubasharAbbas 为什么你严格需要发送 JSON 而不是在接收端创建 JSON?
    【解决方案4】:

    结果在花了两天时间之后,在调用这个方法时,我作为第三个参数(标题)传递了一个令牌,它的末尾有一个 \n(错误地),并且由于对于该字符,请求正文被截断。

    我不知道为什么,但删除 \n 修复了它。

    所以以后如果有人遇到这种情况,你的

    curl 请求正文未发送或被截断

    然后

    查看您的请求标头中不应该存在的特殊字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多