Using cURL to get data

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

40 Replies - 12724 Views - Last Post: 08 July 2010 - 02:31 PM Rate Topic: -----

#1 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Using cURL to get data

Posted 06 July 2010 - 11:30 AM

I have this html file written and uploaded to a commercial server. When the page loads I don't get any error codes, however the php dosen't print out any data.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>


<body>


<h2>Data=</h2>


<?php
// Init $GetData as a cURL object
$GetData = curl_init();

// Tell cURL what URL we want to FTP
$fp = fopen(__FILE__, "r"); 
$url = "ftp://MyFTPpassword:MyFTPuserName@Thumbscalp.com/Signal/test.txt"; 
curl_setopt($GetData, CURLOPT_URL, $url);  

// Tell cURL we would like the results as a string instead of just dumping it on the screen
curl_setopt($GetData, CURLOPT_RETURNTRANSFER, 1);

// Execute the cURL request
$data = curl_exec($GetData);

// Close the cURL request
curl_close($GetData);

// Display the data from the variable.
var_dump($data);

?>


</body>
</html>
 


This post has been edited by inkexit: 06 July 2010 - 11:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using cURL to get data

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: Using cURL to get data

Posted 06 July 2010 - 12:33 PM

If the file is on the same server as your code, why not do this instead?:
$data = file_get_contents("test.txt");


Side note: Using inline php/html is a good way to confuse yourself when the project gets larger!
Was This Post Helpful? 2
  • +
  • -

#3 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Re: Using cURL to get data

Posted 06 July 2010 - 12:50 PM

Well, because I eventually want to have text.txt in a secure folder. I dont't want anything to be able to access it except myself and the php script. So I decided to use FTP. Is there a better way to do this, keeping the file secure?
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Using cURL to get data

Posted 06 July 2010 - 01:11 PM

View Postinkexit, on 06 July 2010 - 12:30 PM, said:

I have this html file written and uploaded to a commercial server. When the page loads I don't get any error codes, however the php dosen't print out any data.


You will likely need to turn on PHP debugging and error messages.

Big questions: what is $fp? Have you defined __FILE__ anywhere?

This post has been edited by Motoma: 06 July 2010 - 01:11 PM

Was This Post Helpful? 2
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: Using cURL to get data

Posted 06 July 2010 - 01:13 PM

  • FTP is not secure.
  • If your php script has access to a folder but users do not, then file_get_contents() is much more secure, since the only way to access the folder is through the script.

Was This Post Helpful? 2
  • +
  • -

#6 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Re: Using cURL to get data

Posted 06 July 2010 - 01:55 PM

forgive me, I'm very new to PHP. I thought FILE was a constant that told fopen what kind of data to fetch. Should I put 'test.txt' in the place of FILE?


$fp = fopen(test.txt, "r");




??
Was This Post Helpful? 0
  • +
  • -

#7 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Re: Using cURL to get data

Posted 06 July 2010 - 02:10 PM

OK, I tunred on error checking, changed the fopen line, and added an echo statement as a back up.
I still get nothing, no data, no errors. nothing from the echo either. Something must be fundamentally wrong here.
This is my complete html with all the php code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>


<body>


<h2>Data=</h2>


<?php

//turn on error reporting
error_reporting(E_ALL); ini_set('display_errors', True); 


// Init $GetData as a cURL object
$GetData = curl_init();

// Tell cURL what URL we want to FTP
$fp = fopen(test.txt, "r"); 
$url = "ftp://MyFTPusername:MyFTPpassword@thumbscalp.com/Signal/test.txt"; 
curl_setopt($GetData, CURLOPT_URL, $url);  

// Tell cURL we would like the results as a string instead of just dumping it on the screen
curl_setopt($GetData, CURLOPT_RETURNTRANSFER, 1);

// Execute the cURL request
$data = curl_exec($GetData);

// Close the cURL request
curl_close($GetData);

// Display the data from the variable to ensure its there.
var_dump($data);

//echo data in case var_dump doesn't work
echo $data;

?>


</body>
</html>
 


Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Using cURL to get data

Posted 06 July 2010 - 02:34 PM

$fp = fopen(test.txt, "r");


test.txt needs to have quotes around it, since it's a string, although I'm not sure why you're opening the file with fopen() but then never using that. cURL does not require fopen() to be used.

In addition, using cURL to handle FTP connections is unnecessary, as PHP has built-in functionality for FTP.

Overall, I agree with CTphpnwb in that file_get_contents() could likely be your best bet if users cannot get to the folder, but the script can. It's faster, it's easier, more secure, and less headache. And if it's that CSV file you were mentioning yesterday (which I would assume it is), you can always run str_getcsv() on it once you read it into a variable, and that'll make your data easier to work with.

I hope this helps :)

This post has been edited by Valek: 06 July 2010 - 02:35 PM

Was This Post Helpful? 1
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: Using cURL to get data

Posted 06 July 2010 - 02:44 PM

Maybe this will make it clearer.

headers.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>


<body>


<h2>Data=</h2>



PHP file:
<?php
error_reporting(E_ALL); ini_set('display_errors', True); 
include("headers.html");
$data = file_get_contents("test.txt");
echo $data;
?>

</body>
</html>
 

Was This Post Helpful? 0
  • +
  • -

#10 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Re: Using cURL to get data

Posted 06 July 2010 - 02:44 PM

Thank you all for all your help.

Valek,

I used fopen because thats how they did it in the tutorial that I read:

http://www.greymass....to-gather-data/

I'm not sure if I exaclty understand how to use file_get_contents().

Would I still use cURL to open up a 'socket' via the php to the html, or can I delete all the cURL stuff from my code?

This post has been edited by inkexit: 06 July 2010 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#11 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Re: Using cURL to get data

Posted 06 July 2010 - 03:04 PM

CTphpnwb,

Are you saing i should create a seperate .php file and another .html file? If so, where would I call the php from? What would my original web page look like? Or would I totally replace it with headers.html?

I created a headers.html file and uploaded it to the server.

I changed the ServerTest.html file to this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>


<body>


<h2>Data=</h2>



<?php  

error_reporting(E_ALL); ini_set('display_errors', True);   
include("headers.html");
$data = file_get_contents("test.txt");  
echo $data;  

?>  




</body>
</html>




I added the php here because I didnt see you call it anywhere.

This still doesnt work. when I load TestServer.htm and/or headers.html I still see nothing but "data=". still no error msgs of any kind... :(
Was This Post Helpful? 0
  • +
  • -

#12 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Using cURL to get data

Posted 06 July 2010 - 03:17 PM

By inline PHP/HTML, he's referring to writing HTML, and breaking in and out of PHP inside of it when you need to do backend stuff or echo variable data, as opposed to having PHP echo the HTML, or using templates.

The tutorial you posted above also doesn't use fopen() anywhere in it, from what I read.

Your script should still be a PHP file, and when using file_get_contents(), you would no longer need to use cURL.

This post has been edited by Valek: 06 July 2010 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: Using cURL to get data

Posted 06 July 2010 - 03:18 PM

You call the php file this way:

http://www.yoursite.com/thephpfile.php

And yes, I'm saying that you should keep php (which runs on the server) separated from html (which is processed by the browser). Or, you can mix them together and confuse yourself!
Was This Post Helpful? 0
  • +
  • -

#14 inkexit  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 05-July 10

Re: Using cURL to get data

Posted 06 July 2010 - 03:33 PM

OK! I GOT IT TO WORK! YEAH!!! YEAH!!! YEAH!!!!!!
SHOTS OF JOHNNY WALKER BLACK ALL AROUND!!

So now I have the php file/page/whatever displaying the contents of the text file. How would I take the $data vairable in the php file and use it to display a certain jpg?

for example:

if($data=="1") //display "1.jpeg"
if($data=="2") //display "2.jpeg"

or would passing the data to javascript using TextResponse=echo be better for this purpose?

This post has been edited by inkexit: 06 July 2010 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#15 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Using cURL to get data

Posted 06 July 2010 - 03:37 PM

You could use a switch, or, seeing as you appear to be using numeric data only for the image display, use is_numeric() to verify that the data is numeric, as you expect. Provided that it is, you could have it drop $data into the src attribute of the image tag in the appropriate place and it'll load dynamically.

Also, I highly recommend looking into a template engine. Smarty is a good one.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3