doujiao1538 2015-06-04 02:58
浏览 46

如何使用curl php在传输之前从数据库生成数组数据

I want to ask about array in php.

I have build curl php and it is successfully transferring the data. But my problem is that I want to transfer that by using array.

This is my code:

<?php
  $sql = "SELECT domain_name  FROM domain";

  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) 
  {
    while($row = mysqli_fetch_assoc($result)) 
    {

      $domain= $row['domain_name'];
      $content= $row['domain_content']; // get values row-wise from db
      $reason = $row['reason'];

      $curlHandle = curl_init();
      curl_setopt($curlHandle, CURLOPT_URL, '192.168.100.2/update.php');
      curl_setopt($curlHandle, CURLOPT_POST, 1);
      curl_setopt($curlHandle, CURLOPT_HEADER, 0);
      curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'domain_name='.$domain.);
      curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION,1); 
      curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER,1);

      if (!curl_exec($curlHandle)) 
      {
        //echo 'everything was successful';
      }
      else 
      {
        echo 'An error has occurred: ' . curl_error($curlHandle);
      } 

    } /* END OF WHILE LOOP */

  } /* END OF NUM ROWS RESULT IS MORE THAN 0 */
  else 
  {
    echo "0 results";
  }
?>

From code above, the system will running in a loop when sending the data until all data from database was transferred.

  • 写回答

1条回答 默认 最新

  • duancan7914 2015-06-04 03:34
    关注

    You can create an array like

    $curl_array = [
      'domain' => $row['domain_name'],
      'content' => $row['domain_content'],
      'reason' => $row['reason']
    ];
    

    if your PHP is older than 5.4 like this

    $curl_array = array(
      'domain' => $row['domain_name'],
      'content' => $row['domain_content'],
      'reason' => $row['reason']
    );
    

    You can read about arrays at http://php.net/manual/en/language.types.array.php

    Also i would suggest JSON encoding (json_encode()) the data before you send it by updating curl_setopt

    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'data='.json_encode($curl_array).);
    

    Note that you would have to update the script 192.168.100.2/update.php to account for array and also to json_decode()

    Eg:

    $curl_post = json_decode($_POST['data']);
    $domain => $curl_post['domain_name'];
    $content => $curl_post['domain_content'];
    $reason => $curl_post['reason'];
    

    Full code:

      <?php
        $sql = "SELECT domain_name  FROM domain";
    
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) 
        {
        while($row = mysqli_fetch_assoc($result)) 
    
        {
    
        // get the values from db ans populate array
        $curl_array = [
          'domain' => $row['domain_name'],
          'content' => $row['domain_content'],
          'reason' => $row['reason']
        ];
    
        $curlHandle = curl_init();
        curl_setopt($curlHandle, CURLOPT_URL, '192.168.100.2/update.php');
        curl_setopt($curlHandle, CURLOPT_POST, 1);
        curl_setopt($curlHandle, CURLOPT_HEADER, 0);
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'data='.json_encode($curl_array).);
        curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION,1); 
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER,1);
    
    
    
        if (!curl_exec($curlHandle)) 
        {
        //echo 'everything was successful';
        }
        else 
        {
        echo 'An error has occurred: ' . curl_error($curlHandle);
        } 
        }
        } 
         else 
        {
        echo "0 results";
        }
      ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛