Bonjour.

If someone could please tell me why the following code works:

<?php
/** URL to babelfish website to scrap **/
  $url2scrap = 'http://babelfish.yahoo.com/translate_txt';

  /** text to translate **/
  $tt_text = urlencode('speak');

  /** set variable to hold what to translate from and to **/
  $tl_value = urlencode('en_fr');
  #function babelScrap($url, $tt_text, $tl_value){

$vars = 'trtext='.$tt_text.'&lp='.$tl_value;

/** create cURL object **/
$ch = curl_init($url2scrap);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);

$return = curl_exec($ch);

curl_close($ch);

/** chop up return to the data we actually want **/
$tmp = split('="result">', $return);
$tmp2 = split('</div>', $tmp[1]);
$tmp3 = split(">", $tmp2[0]);
$return = $tmp3[1];

echo $return;
  #}

  #echo babelScrap($url2scrap, $tt_text, $tl_value);
?>

But this code does not:

<?php
/** URL to babelfish website to scrap **/
  $url2scrap = 'http://babelfish.yahoo.com/translate_txt';

  /** text to translate **/
  $tt_text = urlencode('speak');

  /** set variable to hold what to translate from and to **/
  $tl_value = urlencode('en_fr');
  function babelScrap($url, $tt_text, $tl_value){

$vars = 'trtext='.$tt_text.'&lp='.$tl_value;

/** create cURL object **/
$ch = curl_init($url2scrap);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);

$return = curl_exec($ch);

curl_close($ch);

/** chop up return to the data we actually want **/
$tmp = split('="result">', $return);
$tmp2 = split('</div>', $tmp[1]);
$tmp3 = split(">", $tmp2[0]);
$return = $tmp3[1];

return $return;
  }

  echo babelScrap($url2scrap, $tt_text, $tl_value);
?>

I've got to be missing something obvious. All I am doing is moving my test code into a function.

TIA!

edit

The first set of code displays parlez, which is correct. The second set of code displays a blank page.

    Try this...

    <?php
    /** URL to babelfish website to scrap **/
      $url2scrap = 'http://babelfish.yahoo.com/translate_txt';
    
      /** text to translate **/
      $tt_text = urlencode('speak');
    
      /** set variable to hold what to translate from and to **/
      $tl_value = urlencode('en_fr');
      function babelScrap($url, $tt_text, $tl_value){
    
    $vars = 'trtext='.$tt_text.'&lp='.$tl_value;
    
    /** create cURL object **/
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    
    $return = curl_exec($ch);
    
    curl_close($ch);
    
    /** chop up return to the data we actually want **/
    $tmp = split('="result">', $return);
    $tmp2 = split('</div>', $tmp[1]);
    $tmp3 = split(">", $tmp2[0]);
    $return = $tmp3[1];
    
    return $return;
      }
    
      echo babelScrap($url2scrap, $tt_text, $tl_value);
    ?> 

    Tell me if it works or not. It should. You just forgot to change $url2scrap to $url or vice versa I think.

      I knew it was something retarded.

      Thanks.

        Here is the working code in case anyone wants to scrap a babelfish site ... although I think it is illegal.

        <?php
          /** URL to babelfish website to scrap **/
          $url2scrap = 'http://babelfish.yahoo.com/translate_txt';
        
          /** text to translate **/
          $tt_text = urlencode('speak');
        
          /** set variable to hold what to translate from and to **/
          $tl_value = urlencode('en_fr');
        
          function babelScrap($url, $tt_text, $tl_value){
        
        $vars = 'trtext='.$tt_text.'&lp='.$tl_value;
        
        /** create cURL object **/
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
        
        $return = curl_exec($ch);
        
        curl_close($ch);
        
        /** chop up return to the data we actually want **/
        $tmp = split('="result">', $return);
        $tmp2 = split('</div>', $tmp[1]);
        $tmp3 = split(">", $tmp2[0]);
        $return = $tmp3[1];
        
        return $return;
          }
        
          echo babelScrap($url2scrap, $tt_text, $tl_value);
        ?>
        
          Write a Reply...