[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URL's name in file_get_contents in PHP

Posted on 2011-04-27
14
Medium Priority
?
385 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:duncanb7
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 13

Assisted Solution

by:darren-w-
darren-w- earned 1360 total points
ID: 35473814
$file_url =file_get_contents('http://www.othersite.com/test.aspx?Sybmol='.$a);

Open in new window


your open with a " and closing with a ' amended code above
0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 320 total points
ID: 35473864
constant is a way to use PHP constants dynamicly instead of writing them at parse time. So instead of
if ($condition) $x = A else $x =B;
you could use:
$x = constant($condition ? 'A' : 'B');
Typical you don't use constant at all when programming.

For your error it seems that variable a ist not defined.
0
 
LVL 13

Author Comment

by:duncanb7
ID: 35473869
Could you explain more, What is different form your code to my code about $a
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Author Comment

by:duncanb7
ID: 35473873
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
0
 
LVL 13

Author Comment

by:duncanb7
ID: 35473885
Is it related to the seting in php.ini file ?
0
 
LVL 13

Author Comment

by:duncanb7
ID: 35473889
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35473907
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

0
 
LVL 13

Author Comment

by:duncanb7
ID: 35473995
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()
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 320 total points
ID: 35475279
Buy this book and work through the examples.  From this question and some of your others here at EE I can see that you need some foundation in how PHP works - syntax and structure, etc.  It will really be helpful to you, I promise!
http://www.sitepoint.com/books/phpmysql4/
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35475312
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.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35475344
0
 
LVL 13

Accepted Solution

by:
darren-w- earned 1360 total points
ID: 35475520
<?php

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

?>

Open in new window

Another way of doing it.
0
 
LVL 13

Author Comment

by:duncanb7
ID: 35477065
Be reminded 10 is variable ,
0
 
LVL 13

Author Closing Comment

by:duncanb7
ID: 35477325
Thanks for your reply,

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question