Link to home
Start Free TrialLog in
Avatar of duncanb7
duncanb7

asked on

URL's name in file_get_contents in PHP

Dear Expert,

I am using $file_url =file_get_contents("http://www.othersite.com/test.aspx?Sybmol=10') in php that
is  working fine and I can echo the contents from $file_url,
Since I would like to program the Symbol in Link ,  so we put it into variable such as $a
$a=10;
$file_url =file_get_contents("http://www.othersite.com/test.aspx?Sybmol='.$a);
echo $file_url;
But it fail to echo;  and me give error message  <h1>Bad Request (Invalid Header Name)</h1>
and I  try to $file_url =file_get_contents("http://www.othersite.com/test.aspx?Sybmol='.constant($a));
and it is also failed and warn it constant() is not made;

Why? Please advise
Also I want to know when we will use constant() in php, what is advantage over using variable ?

Duncan
SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duncanb7
duncanb7

ASKER

Could you explain more, What is different form your code to my code about $a
Do we need to define varaible.

I get used to for example, if need a variable just typing

$a;
$b= $a+1;

I never use Dim to define variable like in VBA
Is it related to the seting in php.ini file ?
Could you explain more, What is different form your code to my code about $a

I open and close the speech marks using the same type of paresis

ie :
$a = 10; //define $a to be 10
$b='http://www.othersite.com/test.aspx?Sybmol='.$a;  //$b = http://www.othersite.com/test.aspx?Sybmol=10
$file_url =file_get_contents($b);  //gets the content returned into $file_url

Open in new window


You don't need to use dim, http://www.tizag.com/phpT/variable.php

I tried this before and it is also fail that is why I put back 'http://www.othersite.com/test.aspx?Sybmol='
int file_get_contents()
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might use something like this to make your URL string visible.

Instead of this...
$a=10;
$file_url =file_get_contents("http://www.othersite.com/test.aspx?Sybmol='.$a);
echo $file_url;

Try this...
$a=10;
$url = "http://www.othersite.com/test.aspx?Sybmol=$a";
echo $url;
$file_url =file_get_contents($url);
echo $file_url;

You might also want to check the spelling of Sybmol vs Symbol.  You might also want to learn about this function:
http://php.net/manual/en/function.urlencode.php

There is also the possibility that the GET argument keys are case-sensitive, so Symbol would be different from symbol.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Be reminded 10 is variable ,
Thanks for your reply,

Only
$var="0"
$data = array('Symbol'=>$var);
$url = "http://www.othersite.com/test.aspx?".http_build_query($data);
echo $url;