从POSTMAN生成的PHP cURL可用于邮递员,但不适用于php代码

时间:2020-03-04 10:47:41

标签: php curl

我已经从cURL导出了POST POSTMAN,在这里工作正常,但是当我尝试以php的身份运行它时,出现了错误丢失,这是else中有关参数的if(isiset..

邮递员生成的cURL:

<?php 

class trimitereNotificare_app {

public function send($email_utilizator, $imagine) {

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost/sendSinglePush.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => array ('title' => 'CityAlert', 'message' => 'blalvlv', 'email' => "$email_utilizator", 'image' => "$imagine"),
  CURLOPT_HTTPHEADER => array(
    "Content-Type: multipart/form-data; boundary=--------------------------748736578315812240456685"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
echo $email_utilizator;
echo $imagine;

}
}

回声:

{"error":true,"message":"Parameters missing"}
irina@yahoo.com
scopesystems.ro/scopesystems.ro/teste/Pictograme/Strazi si trotuare/

作为我传递给函数的数据,这是正确的。

.php sendSinglePush:

if($_SERVER['REQUEST_METHOD']=='POST'){ 
//hecking the required params 
if(isset($_POST['title']) and isset($_POST['message']) and isset($_POST['email'])){

    //creating a new push
    $push = null; 
    //first check if the push has an image with it
    if(isset($_POST['image'])){
        $push = new Push(
                $_POST['title'],
                $_POST['message'],
                $_POST['image']
            );
    }else{
        //if the push don't have an image give null in place of image
        $push = new Push(
                $_POST['title'],
                $_POST['message'],
                null
            );
    }

    //getting the push from push object
    $mPushNotification = $push->getPush(); 

    //getting the token from database object 
    $devicetoken = $db->getTokenByEmail($_POST['email']);

    //creating firebase class object 
    $firebase = new Firebase(); 

    //sending push notification and displaying result 
    echo $firebase->send($devicetoken, $mPushNotification);
}else{
    $response['error']=true;
        $response['message']='Parameters missing';
    }
}else{
    $response['error']=true;
    $response['message']='Invalid request';
}

如开头所述,第二个php表示缺少参数,即使在CURLOPT_POSTFIELDS中我添加了所需的参数。

2 个答案:

答案 0 :(得分:1)

我设法通过将cURL php更改为:

  class trimitereNotificare_app {

    public function send($email_utilizator, $imagine) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,            "http://localhost/sendSinglePush.php" );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS,     array('title' => 'CityAlert','email' => $email_utilizator,'message' => 'O alertă a fost modificată!','image' => $imagine) ); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: multipart/form-data')); 
    $result=curl_exec ($ch);
    curl_close($ch);
}
}

答案 1 :(得分:0)

如果有人遇到类似问题,Postman还添加了“新代码生成模式”,可以通过设置启用它-如果您使用的是最新版本v.7.19,则该功能已启用。

了解更多:https://support.getpostman.com/hc/en-us/articles/360036521074-What-is-New-Code-Generation-mode-

Settings> New Code generation mode