dongreng9864 2015-06-08 13:33
浏览 52
已采纳

在php中的数组中发布数据

I have collected data and created and array :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Martin
            [surname] => test
            [email] => martin@gmail.com
            [dob] => 2015-02-24
        )

    [1] => Array
        (
            [id] => 2
            [name] => Kary
            [surname] => paulman
            [email] => kary@gmail.com
            [dob] => 2015-06-26
        )

)

I have multiple records in this array.

I want to post each record in the array to www.recieve.com , where it will pass a response of 'true' if post was successful and 'false' if it fails.

I have researched the interent and i dont even know where to start.

So far my code looks like this (this is just for the array)

$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}

echo "<pre>"; print_r($res);   echo "</pre>";

I have tryed this and it is not working : 

//Build my array
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}

//URL to post to
$url = 'https://theurl.com?';

//url-ify the data for the POST
foreach($res as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
  • 写回答

2条回答 默认 最新

  • dtmooir3395 2015-06-08 15:20
    关注

    Use Curl and like this

    $ch = curl_init();                    // initiate curl
    $url = "http://www.somesite.com/curl_example.php"; // where you want to post data
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);  // tell curl you want to post something
    curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n"); // define what you want to post
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
    $output = curl_exec ($ch); // execute
    
    curl_close ($ch); // close curl handle
    
    var_dump($output); // show output
    
    ?>
    

    Use your array in the : curl_setopt($ch, CURLOPT_POSTFIELDS

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料