0

I need to send a data via cURL in PHP. There is my POST request and it works with this example (I can post 1 record).

 $postData = array(
           "username" => "[email protected]",
            "name" => "Name",
            "surname" => "Surname",
            'role' => 'user',
            "disabled" => "false"


      );

        $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 curl_setopt_array($ch, 
               array(
                   CURLOPT_POST => false,
                   CURLOPT_RETURNTRANSFER => TRUE,
                   CURLOPT_HTTPHEADER => array(
                    'authorization: apikey '.$apikey,
                    'Content-Type: application/rest+json',
                    'pincode: '.$pincode
                                              )
                  // ,CURLOPT_POSTFIELDS => json_encode($postData)
   ));
 curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
 $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
 $response = curl_exec($ch);
 if ($response === false) $response = curl_error($ch);
 return stripslashes($response);
 curl_close($ch);

But I would like to add records from database into $postData array but then it's stops.

This is my database array example:

$postData = array();

  foreach ($records = $this->getRecords as $value) {

        $row['username'] = $value['email'];
        $row['name'] = $value['name'];
        $row['surname'] = $value['surname'];
        $row['role'] = 'User';
        $row['disabled'] = $value['status'];

        array_push($postData, $row); 



      }

json array looks ok but I can't find an error. Or i can't post multiple data and i need to add everything in a loop?

1 Answer 1

1

I find a few flaws in the code. You said that you are posting the code but you have set

CURLOPT_POST => false

Also, you have repeated

CURLOPT_RETURNTRANSFER => TRUE and CURLOPT_RETURNTRANSFER => 1

Also, your API might be designed to accept one record but you are trying to send multiple records. If it is designed to accept 1 record at a time you should make the curl call a function and call it in place of array_push. eg:. curlfunction($row);

 function curlfunction($postData){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST , 1);
    curl_setopt_array($ch, CURLOPT_HTTPHEADER, array(
                'authorization: apikey '.$apikey,
                'Content-Type: application/rest+json',
                'pincode: '.$pincode
             )

      );
      curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
      $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); 
      $response = curl_exec($ch);
      if ($response === false) $response = curl_error($ch);
      curl_close($ch);
      return stripslashes($response);
  }

and

  foreach ($records = $this->getRecords as $value) {

    $row['username'] = $value['email'];
    $row['name'] = $value['name'];
    $row['surname'] = $value['surname'];
    $row['role'] = 'User';
    $row['disabled'] = $value['status'];

    curlfunction($row); 



  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.