【问题标题】:cleaning a post URL from an Array从数组中清除帖子 URL
【发布时间】:2015-06-09 08:01:25
【问题描述】:

这是我的代码:

$query = "SELECT first_name, surname, email FROM app2";
$result = mysql_query($query) or die(mysql_error());

 $url = "https://test.com?"; // Where you want to post data
 while($row = mysql_fetch_array($result)) {
        $input = "";
        foreach($row as $key => $value) $input .= $key . '=' . urlencode($value) . '&';
        $input = rtrim($input, '& ');

        $ch = curl_init();                    // Initiate cURL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);  // Tell cURL you want to post something
        curl_setopt($ch, CURLOPT_POSTFIELDS, $input); // 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
    }

问题是数据($input)是这样出来的:

0=Lucky&first_name=Lucky&1=Dube&surname=Dube

应该是这样的:

first_name=Lucky&surname=Dube

【问题讨论】:

    标签: php post curl clean-urls


    【解决方案1】:

    无需自行构建查询。首先,只需使用_assoc()* 函数风味,然后在该行数组批处理上使用http_build_query,然后喂它。它将为您构造查询字符串。无需将&foreach 附加到每个元素中。粗略的例子:

    $url = "https://test.com"; // 你想在哪里发布数据 而($row = mysql_fetch_assoc($result)) { $input = http_build_query($row); $ch = curl_init(); // 启动 cURL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); // 告诉 cURL 你想发布一些东西 curl_setopt($ch, CURLOPT_POSTFIELDS, $input); // 定义你要发布的内容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 以字符串格式返回输出 $output = curl_exec ($ch); // 执行 curl_close ($ch); // 关闭 cURL 句柄 }

    【讨论】:

    • @MatHatrik 它仍在mysql_fetch_array 中,因此它返回一个数字索引数组。使用mysql_fetch_assoc
    • 谢谢! .它返回的电子邮件地址为:longmartin%40gmail.com,我怎样才能让它正常返回:longmartin@gmail.com
    【解决方案2】:

    尝试改变

    while($row = mysql_fetch_array($result)) {
    

    while($row = mysql_fetch_assoc($result)) {
    

    mysql_fetch_assoc 只返回关联数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多