[ANSWERED] php – Get return value from URL

Solution : 1 :

If you want to retrieve the contents of a url, you can use the file_get_contents() function. The implementation would look something like this:

$url = 'http://example.com/weight';
$weight = file_get_contents($url);
echo $weight;

Also, you might want to use a function like floatval() to convert your result to a float.
Read more about file_get_contents() here: https://www.w3schools.com/php/func_filesystem_file_get_contents.asp

Problem :

I have access to a URL that if i simply put http://example.com/weight into address bar it returns a float number…

Ex: 17.23kg

Image for better understanding:

What happens if i go to URL

My objective is to use PHP for assigning this float value into a variable.
$weight = float value…

Here is the code i did with hopes that it would work:

$url = 'http://example.com/weight';
$url_components = parse_url($url);
parse_str($url_components['weight'], $params);
$weight = $params['weight'];
echo $weight;

But this returned nothing…

What am i doing wrong?

Problem :

I have access to a URL that if i simply put http://example.com/weight into address bar it returns a float number…

Ex: 17.23kg

Image for better understanding:

What happens if i go to URL

My objective is to use PHP for assigning this float value into a variable.
$weight = float value…

Here is the code i did with hopes that it would work:

$url = 'http://example.com/weight';
$url_components = parse_url($url);
parse_str($url_components['weight'], $params);
$weight = $params['weight'];
echo $weight;

But this returned nothing…

What am i doing wrong?

Comments

Comment posted by enabled error reporting

It would return quite a bit if you

Comment posted by Raul Chiarella

Hello! I enabled error logging and it returned ‘Index was not found’ … I will take a look into file_get_contents() and see if i can come up with a code that gets the value from the URL entered.

Comment posted by Ronaldo Ferreira de Lima

It’s confusing, the image provided suggests that the value you are looking for is in the body of the reply (as image, html or any other possible think) and not int URL itself which is where your code are trying to extract the information.

READ  [ANSWERED] Unable to upload files on Google Cloud Storage using PHP via Google Compute Engine

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *