0

I am learning about web service and I had a problem trying to consume a static data file data.txt

this is my index.php

<?php 
 
 $curl = curl_init("http://localhost/web_service/base.txt");

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

 $answer = curl_exec($curl);
 $info = curl_getinfo($curl);

 if ($info['http_code' == 200] ){
  
  $data = explode(",", $answer);

  foreach ($data as $fruit) {
   echo "-> ". $fruit ."<br>";
  }
  
 } else {

  echo "error" . curl_error($curl);
 }

?>

And my base.txt contains only

Banana, Apple, watermelon

this is the error:

The localhost page is not working

Localhost is unable to process this request at this time.

HTTP ERROR 500

I do not understand this error, my apache server is started.

I hope can you help me :)

  • can you access "http://localhost/web_service/base.txt" in your browser? – Dan Ionescu Mar 06 '17 at 17:22
  • Check your server error logs to find out why it's throwing a 500 error. – aynber Mar 06 '17 at 17:23
  • [:error] [pid 1962] [client ::1:51590] PHP Fatal error: Uncaught Error: Call to undefined function curl_init() in /var/www/html/web_service_oncti/index.php:3\nStack trace:\n#0 {main}\n thrown in /var/www/html/web_service_oncti/index.php on line 3 (( This throws me the log )) ************ When I try to access localhost / web_service / base.txt it throws me "The requested URL /web_service/base.txt was not found on this server." – Arnell Vasquez Corona Mar 06 '17 at 17:33
  • You need to install php curl. – aynber Mar 06 '17 at 17:36
  • Possible duplicate of [curl\_init() function not working](http://stackoverflow.com/questions/4477535/curl-init-function-not-working) – aynber Mar 06 '17 at 17:37
  • It worked, I had no Curl installed, I forgot I was on another computer (How silly). I had given an error on line 10, where the IF conditional is located, it was because I had defined ($ info ['http_code' == 200]) and it was wrong. Many thanks!! How do I put the approval that this problem has been solved? – Arnell Vasquez Corona Mar 06 '17 at 19:50

0 Answers0