cURL 和 PHP 显示“1"

编程入门 行业动态 更新时间:2024-10-16 16:30:31
本文介绍了cURL 和 PHP 显示“1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 PHP 脚本,我想用它从数据库读取服务器并使用 cURL 连接到它们.服务器响应来自 sql 查询的结果.问题是服务器每次响应后的脚本显示数字 1.输出如下所示:

I have a PHP script with which I want to read servers from database and connect to them with cURL. Servers responds with results from sql query. The problem is that script after each respond from server displays number 1. The ouput looks like this:

服务器 1:一些结果

1Server 2:一些结果

1Server 2: some results

1Server 3:一些结果

1Server 3: some results

1

这是从数据库读取服务器并连接到它们的代码:

Here is the code that reads servers from database and connects to them:

<?php $mysql_id = mysql_connect('localhost', 'ms', 'pass'); mysql_select_db('servers', $mysql_id); mysql_query("SET NAMES utf8"); $query = "SELECT * FROM svr"; $result = mysql_query($query); $num = mysql_num_rows($result); while ($data = mysql_fetch_assoc($result)) { $server[] = $data; } mysql_close($mysql_id); $i = 0; while($i < $num) { $dealer = $server[$i]['dealer']; echo $dealer . "<br />"; $data = "val=a"; //just for testing $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text/html; charset=utf-8') ); $result = curl_exec($ch); echo $result; $i++; } ?>

我发现 1 显示为 "echo $result;"创建响应的代码是这样的:

I discovered that 1 is displayed with "echo $result;" and the code for creating response is this:

<?php $mysql_id1 = mysql_connect('localhost', 'ms', 'pass'); mysql_select_db('servers', $mysql_id1); mysql_query("SET NAMES utf8"); $query2 = "SELECT * FROM data"; $result2 = mysql_query($query2); $num2 = mysql_num_rows($result2); while ($data2 = mysql_fetch_assoc($result2)) { $deli[] = $data2; } $i1 = 0; $space = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; while ($i1 < $num2) { echo $space . $deli[$i1]['id'] . " "; echo $deli[$i1]['artikel'] . " "; echo $deli[$i1]['znamka'] . " "; echo $deli[$i1]['model'] . " "; echo $deli[$i1]['letnik'] . " "; echo $deli[$i1]['cena'] . " € "; echo $deli[$i1]['zaloga'] . "<br />"; $i1++; } echo "<br />"; mysql_close($mysql_id1); ?>

请帮帮我

推荐答案

使用 CURLOPT_RETURNTRANSFER 选项.否则 cURL 会自动回显数据,只返回 true(通过 echo 转换为 1).

Use the CURLOPT_RETURNTRANSFER option. Otherwise cURL will automatically echo the data and just return true (which is converted to 1 by echo).

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

PHP 说,

TRUE 将传输作为 curl_exec() 的返回值的字符串返回,而不是直接输出.

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

更多推荐

cURL 和 PHP 显示“1"

本文发布于:2023-06-06 00:33:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/531158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:cURL   PHP   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!