how to Capture Html using PHP

Discussion in 'PHP' started by Hamid Farhan, Aug 13, 2012.

  1. #1
    here is it what i want:-

    1) Send a request to website using php. (like www.xyz.com?id=2)
    2) Capture the response ans store it in a string
     
    Hamid Farhan, Aug 13, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    best way to do it is with curl.

    curl[dot]haxx[dot]se (sorry can't post links yet) is probably the best resource with loads of examples
     
    plussy, Aug 13, 2012 IP
  3. Shiplu

    Shiplu Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Did you google for this?

    If you have lib curl installed you can use curl which is allowed in shard host. If you have vps or dedicated server you should use url wrapper provided by PHP. Its built in PHP so its quite easy.
    But curl is your best bet. Its enabled in almost 99% of LAMP server. you can google "PHP Curl example" and you'll fine numerous examples.

    I use this PHP class. WebGet. It supports file system caching and provides the response in a good structure so you can parse it easily later. See this example how craiglist is being parsed.
     
    Shiplu, Aug 13, 2012 IP
  4. lazyspider

    lazyspider Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    file_get_contents(url) is good enough if you have allow_url_fopen is enabled in your php hosting otherwise use curl.
     
    lazyspider, Aug 13, 2012 IP
  5. ibnatu

    ibnatu Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i would use file_get_contents, it's simple enough.
     
    ibnatu, Aug 14, 2012 IP
  6. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    I like the cURL approach, since it's possible to modify a lot of settings that are sometimes required, and not possible to do with a simple file_get_contents.
    Something like this should get you started:
    
    $url = "http://xyz.com";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // curl_setopt($ch, CURLOPT_HEADER, true); // Enable this if you want the HTTP Headers returned
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
    Code (markup):
    Using cURL you could, for example, modify the HTTP Headers you're sending, create SSL connections that're properly verified, use cookies and other things. You can also use it to log into other websites and pull data.
     
    Soulstone, Aug 14, 2012 IP
  7. shubhamm

    shubhamm Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    shubhamm, Aug 14, 2012 IP