Beginner Question: How To POST a url within code

Discussion in 'PHP' started by timallard, Dec 8, 2010.

  1. #1
    Hi Everyone,

    This seems to be a simple question. But I am new to JSON and I am working with PHP.
    I have my json formatted string which i need to pass to gain access to an API but I do not want this in the URL bar...

    I then need to dynamically manipulate that url to pass other data.

    My question is, if i have a URL: www.example.com?username=something&password=somethingelse

    how can I call that url to load my information when my page loads with index.php?
     
    timallard, Dec 8, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    search for CURL on php.net
     
    tvoodoo, Dec 8, 2010 IP
  3. hardik_dan

    hardik_dan Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can use CURL or php's function get_url_contents to fetch the url content into php variable
     
    hardik_dan, Dec 9, 2010 IP
  4. backlinkneeded

    backlinkneeded Member

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    wow didnt know this before. so Curl fetches the url content.
     
    backlinkneeded, Dec 22, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    which is why its called cURL
     
    danx10, Dec 22, 2010 IP
  6. tprinty

    tprinty Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if you don't have curl you could exec the wget command with the correct URL.
     
    tprinty, Jan 2, 2011 IP
  7. umarsa

    umarsa Well-Known Member

    Messages:
    94
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #7
    This is very simple to do within P H P via cURL. Here is something i wrote up real quick for you.

    $c = curl_init('http://www.example.com/index.php');
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($c, CURLOPT_USERAGENT, "A useragent here" );
    curl_setopt($c, CURLOPT_POST, 1);
    curl_setopt($c, CURLOPT_POSTFIELDS, "username=something&password=somethingelse");
    $output = curl_exec($c); 
    Code (markup):
    You can then use the following code the echo the output:
    echo $output
    Code (markup):
    I hope that helps :)
     
    Last edited: Jan 2, 2011
    umarsa, Jan 2, 2011 IP