phpfilecurlfile-get-contentsphp-parser

Parse Web Page content using PHP


I think it's simple question but I've done what I know and still not work. I want get output from this link :

http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en

You can paste on the browser and look into it. There some text output. I've tried with some function in PHP like file_get_contents and curl. I'm not using ajax or JavaScript because I'm not expert with it. And the last, I'm working with XAMPP.


Solution

  • $url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?text=siapa+rektor+ipb&appId=58C40548A812ED699C35664525D8A8104D3006D2&from=id&to=en';
    
    // using file_get_contents function
    $content = file_get_contents($url);
    echo $content;
    #output# "who is the Rector of the University"
    
    // using file function // read line by line in array
    $content = file($url);
    print_r($content);
    
    #output# Array (0] => "who is the Rector of the University")
    
    // using cURL
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_HEADER, 0);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $content = curl_exec($ch);
    echo $content;
    #output# "who is the Rector of the University"