PHP function in file_get_contents URL

Asked

Viewed 68 times

0

I have the following code:

    $ws1 = file_get_contents('http://10.0.0.0:1111/bids/all/'.$obj->auction_id);

How do I include one more bar (/) after $obj->id and put one more PHP code like this:

http://10.0.0.0:1111/bids/all/PHP1/PHP2

1 answer

3


Following the same logic:

$ws1 = file_get_contents('http://10.0.0.0:1111/bids/all/'.$obj->auction_id.'/'.$php2);
//                                                                 Barra ---^
//                                                            Outra variável ---^

In PHP you use the . to concatenate (join) two strings.

Change the variable $php2 by whatever after the bar (for example, $obj->outro_campo).

As commented by our colleague @Valdeirpsr, there is this other way to interpolate:

file_get_contents("http://10.0.0.0:1111/bids/all/{$obj->auction_id}/$php2");

Whenever a string is delimited by double quotes, PHP interprets the names started by $ as variables. In the case of objects and some expressions, to disambiguate, it is necessary to use the delimiters { } around the set.

  • 1

    Or else file_get_contents("http://10.0.0.0:1111/bids/all/{$obj->auction_id}/{$php2}");

  • @Valdeirpsr added to be more complete

Browser other questions tagged

You are not signed in. Login or sign up in order to post.