14 Kasım 2009 Cumartesi

PHP ve curl ile form gönderme örneği

  $cnt = 0;
  $path = "./list.csv";
  $handle = fopen($data_file, "r");
  $lines=file($data_file);

  $idx = 0;
  $line = $lines[$i];
  $licensePre = 0;

  while (is_null($lines[$idx]) != true)
    {
      $line = $lines[$idx];
      $raw = explode(',',$line);


      if ($licensePre == (int)$raw[0])
    {
      $idx = $idx + 1;
      continue;
    }

      $url = "http://www.*****.***/****/";
      $formUrl = '*****.php?tpl=***&main=33';

      $nameSurname = explode(' ',$raw[1]);
      $nameSize = count($nameSurname);
      $name = $nameSurname[0];
      $ns = 1;

      if ($nameSize > 2)
        {
          for ($ns=1; $ns<$nameSize-1; $ns++)
        $name = $name . " " . $nameSurname[$ns];
        }

      $surname = $nameSurname[$nameSize - 1];
      $licenseNo = $raw[0];
      $teamName = $raw[5];

      $rawLicenseDate = explode('/',$raw[4]);
      $rawBirthDate = explode('/',$raw[2]);

      $birthPlace = ($raw[3]) ? $raw[3] : '            ';

      $licenseDay = ($rawLicenseDate[1]) ? $rawLicenseDate[1] : '';
      $licenseMonth = ($rawLicenseDate[0]) ? $rawLicenseDate[0] : '';
      $licenseYear = ($rawLicenseDate[2]) ? $rawLicenseDate[2] : '';
      $birthDay = ($rawBirthDate[1]) ? $rawBirthDate[1] : '';
      $birthMonth = ($rawBirthDate[0]) ? $rawBirthDate[0] : '';
      $birthYear = ($rawBirthDate[2]) ? $rawBirthDate[2] : '';

      $filename = "./pic/{$licenseNo}.jpg";

      if (file_exists($filename))
        $img = "@{$filename}";
      else
        $img = '';

      $data = array(
          'emails' => '****',
          'password' => '***',
          'txtPlayerName' => $name,
          'txtPlayerSurname' => $surname,
          'txtLicenseNo' => $licenseNo,
          'playerPosition' => '0',
          'teamName' => (int)$teamName,
          'national_team_id' => '-1',
          'license_given_day' => $licenseDay,
          'license_given_month' => $licenseMonth,
          'license_given_year' => $licenseYear,
          'license_end_day' => '',
          'license_end_month' => '',
          'license_end_year' => '',
          'birth_day' => $birthDay,
          'birth_month' => $birthMonth,
          'birth_year' => $birthYear,
          'placeOfBirth' => $birthPlace,
          'btnSubmit' => 'Submit >>',
          'image' => $img,
      );

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url . $formUrl);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

      $curlrt = curl_exec($ch);
      $licensePre = $licenseNo;

      $cnt++;

      echo $cnt . "   " . $line . "\n";
      $idx = $idx + 1;
    }
?>

Konular

Matematik (5) Kod (4) Gündem (2) Bilgisayar (1) İnternet (1)