使用未定义的常量CURL_POSTFIELDS-假定为“ CURL_POSTFIELDS”(这将在以后的PHP版本中引发错误)

时间:2019-06-06 07:22:57

标签: laravel curl post sms undefined

我正在编写一个laravel应用程序发送短信。但是,postfields部分抛出错误。我该如何解决?

private function sendMessage($message, $recipients) {
    $encodeMessage=urlencode($message);
    $authkey = 'XYZ';
    $senderid = '';
    $route = ;
    $country = ;
    $data = array(
        'authkey' => $authkey,
        'recipients' => $recipients,
        'message' => $encodeMessage,
        'sender' => $senderid,
        'route' => $route,
        'country' => $country,
    );
    //dd($recipients)
    $url = " ";
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURL_POSTFIELDS => $data
    ));
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);$output=curl_exec($ch);
    if(curl_errno($ch)) {
        echo 'error '.curl_error($ch);
    }
    curl_close($ch);
    return back()->with('success','Messages sent successfully');
}

这是我得到的错误:

  

“使用未定义的常量CURL_POSTFIELDS-假定为'CURL_POSTFIELDS'   (这将在将来的PHP版本中引发错误)”

2 个答案:

答案 0 :(得分:1)

您的错误表明您使用了一个未定义的常量,这意味着它从未声明过。确实,正如评论中已经提到的那样,正确的常数是CURLOPT_POSTFIELDS

答案 1 :(得分:-1)

$fileName = storage_path('app') . '/tmp.xlsx';
$file_put_contents($fileName, file_get_contents($path));
$fields['file_name'] = $this->makeCurlFile($fileName);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

protected function makeCurlFile($file)
{
   $mime = mime_content_type($file);
   $info = pathinfo($file);
   $name = $info['basename'];
   $output = new \CURLFile($file, $mime, $name);
   return $output;
}