Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL and PHP displaying "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:

Server 1: some results

1Server 2: some results

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++; }  ?> 

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); ?> 

Please help me

like image 404
Soriyyx Avatar asked Jun 06 '12 06:06

Soriyyx


People also ask

Does cURL work in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.

How can get cURL value in PHP?

php $url = 'hxxp://domain.com/univ/v8?q=tas+wanita'; $ch=curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $r=curl_exec($ch); curl_close($ch); print_r(json_decode($r, true)); ?>

Which cURL does PHP use?

cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual. In order to use PHP's cURL functions you need to install the » libcurl package. PHP requires that you use libcurl 7.0.


1 Answers

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.net says,

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

like image 80
Emil Vikström Avatar answered Oct 11 '22 04:10

Emil Vikström