繁体   English   中英

CURL 发布请求到 API 循环通过数据库

[英]CURL Post Request to API Looping Through Database

I've been trying to select values (students data) from mysql database table and looping through database to send to an API using PHP CURL Post request but it's not working.

这是 API 主体:

{
    "students":[
        {
            "admissionNumber": "2010",
            "class":"js one"
        },
        {
            "admissionNumber": "2020",
            "class":"ss one"
        }
    ],
    "appDomain":"www.schooldomain.com"
}

我要发送的参数是“admissionNumber”和“class”参数,而“appDomain”对所有参数都是一样的。 这是我的代码:

if(isset($_POST['submit'])){

       $body = "success";
       $info = "yes";


      class SendDATA
      {
        private $url = 'https://url-of-the-endpoint'; 
        private $username = '';  
        private $appDomain = 'http://schooldomain.com/';  



        // public function to commit the send
        public function send($admNo,$class)
        {
            $url_array=     array('admissionNumber'=>$admNo,'class'=>$class,'appDomain'=>$this-> appDomain);
            $url_string = $data = http_build_query($url_array);

            //   using the curl library to make the request
            $curlHandle = curl_init();
            curl_setopt($curlHandle, CURLOPT_URL, $this->url);
            curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string);
            curl_setopt($curlHandle, CURLOPT_POST, 1);
            $responseBody = curl_exec($curlHandle);
            $responseInfo  = curl_getinfo($curlHandle);
            curl_close($curlHandle);

            return $this->handleResponse($responseBody,$responseInfo);
        }


        private function handleResponse($body,$info)
        {
            if ($info['http_code']==200){ // successful submission
                $xml_obj = simplexml_load_string($body);
                // extract 

                return true;
            }
            else{

                // error handling
                return false;
            }

        }

    }

    $sms = new SendDATA();

        $result = mysqli_query( $mysqli, "SELECT * FROM school_kids");

        while ($row = mysqli_fetch_array($result)) {
                $admNo = $row['admNo']; 
                $class = $row['class']; 
                $sms->send($admNo,$class,"header");

                echo $admNo. " ".$class;  

        }                                       
   }

这个问题相当不清楚。 when you say "this is the API body", I presume this JSON fragment is what the REST API at https://url-of-the-endpoint expects. 如果是这样,那么您构建的请求正文是错误的。 http_build_query创建一个 URL 编码的表单数据块(如key=value&anotherKey=another_value ),而不是 JSON。 对于 JSON,这就是您想要的:

$data = array('students' => array
(
    array('admissionNumber' => $admNo, 'class' => $class)
),
'appDomain':$this->appDomain
);
$url_string = $data = json_encode($data);

此外,您可能希望从响应中删除 HTTP 标头:

curl_setopt($curlHandle, CURLOPT_HEADER, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM