从POSTMAN生成的PHP cURL可用于postman,但不能用于php代码

s4chpxco  于 2022-11-13  发布在  Postman
关注(0)|答案(2)|浏览(330)

我已经从POSTMAN导出了一个cURLPOST,在那里它工作得很好,但是当我试图以php的形式运行它时,我得到了参数丢失的错误,这是从if(isiset..导出的关于参数的else
cURL由 Postman 生成:

<?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中添加了所需的参数。

kqlmhetl

kqlmhetl1#

我已设法解决此问题,方法是将cURLphp更改为:

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);
}
}
v09wglhw

v09wglhw2#

此外,如果有人面临类似的问题,波兹曼增加了“新代码生成模式”,可以从设置启用-如果你是在最新版本的v.7.19,这是已经启用。
了解更多信息:https://support.getpostman.com/hc/en-us/articles/360036521074-What-is-New-Code-Generation-mode-

相关问题