cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Curl PHP Translation

From: Wes _at_ Kode Host <_at_>
Date: Mon, 5 Jun 2006 17:26:34 +0700

Newbie on the case.

Does anyone now how to translate this script to php?

I basically want to be able to log in to the secure server, then get the
call records.

For more info please contact me back on wes_at_kodehost.co.uk

If anyone can build this script for me I will pay (neg.)

Cheers
Wes

Start ---->

#!/bin/sh
# 27 May 2006

OP="$1"
SERVICE="$2"
USERNAME="$3"
PASSWORD="$4"
MONTHSAGO="$5"
DAYSAGO="$5"
# uncomment the following line if usage is accounted for on "per minute or
part thereof" basis
TOTALIZER='{totalsecs +=(3600 * $1 + 60 * ($2 + int(($3 + 59)/60)))} END
{print totalsecs/60}'
# uncomment the following line if usage is accounted for on per second basis
#TOTALIZER='{totalsecs += (3600 * $1 + 60 * $2 + $3)} END {print
int((totalsecs+59)/60)}'
usage() {
 echo >&2 "usage: $(basename $0) [creditcents|usedminutes|last7daysminutes]
service username password [monthsago|daysago]"
 exit 255
}

Curl() {
 curl "$@"
 local STATUS="$?"
 if [ ! $STATUS ]; then rm -f ${COOKIEJAR}; exit $STATUS; fi
}

case _"$OP" in
_creditcents)
;;
_usedminutes)
;;
_last7daysminutes)
 NOW=$(awk 'BEGIN{print systime()}')
 if [ $? != 0 ]; then
  echo >&2 "This script requires awk installed".
  exit 255
 fi
;;
*)
 usage
;;
esac

case _"$PASSWORD" in
_)
        usage
;;
esac

gomainpage() {
 LOGGEDIN=$(Curl -s -k -b ${COOKIEJAR} \
          "${BASEURL}"'/index.php?part=menu&justloggedin=true')
}

BASEURL="https://myaccount.${SERVICE}.com/clx"
COOKIEJAR="/tmp/cj.$$"
CALLRECORDS="/tmp/cr.$$"
umask 077 # protect cookie jar from indiscreet eyes
#### Login with username and password
LOGGEDIN=$(Curl -s -k -c ${COOKIEJAR} -d username="${USERNAME}" -d
password="${PASSWORD}" ${BASEURL}/ | grep -i 'Refresh')
if [ _"$LOGGEDIN" = "_" ]; then exit 254; fi
#### go to index page as required by META Refresh. This time will fail (?)
gomainpage
case _"$OP" in
_creditcents)
#### go to index page as required by META Refresh. This time will work (?)
 gomainpage
 echo $LOGGEDIN \
 | sed -n -e 's|.*Remaining credit[^0-9]\+\([0-9.]\+\).*|\1|p' \
 | awk '{print 100 * $1}'
;;
_usedminutes)
 CURYEAR=$(date +%Y)
 CURMONTH=$(date +%m | sed -e 's/^0//')
 if [ _"$MONTHSAGO" != _ ]; then
  CURABSMONTH=$((12 * $CURYEAR + $CURMONTH - 1))
  THENABSMONTH=$(($CURABSMONTH - $MONTHSAGO))
  CURYEAR=$(($THENABSMONTH / 12))
  CURMONTH=$((($THENABSMONTH % 12) + 1))
 fi
 Curl -s -k -b ${COOKIEJAR}
"${BASEURL}/calls.php?month=${CURMONTH}&year=${CURYEAR}" \
 | sed -n -e \
 
's|[[:space:]]*<tr>.*\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\).*FREE.*|\1
\2 \3|p' \
 | awk "$TOTALIZER"
 #### go to index page before logoff (?)
 gomainpage
;;
_last7daysminutes)

 datedaysago() { # format days_ago [timestamp]
  local TS="$3"
  if [ _"$TS" = _ ]; then TS=$(awk 'BEGIN{print systime()}'); fi
  awk 'BEGIN{print strftime("'"$1"'",'"$TS"'-('"$2"'*24*3600))}'
 }

 if [ _"$DAYSAGO" = _ ]; then
  DAYSAGO=0
 fi

 YEAREND=$(datedaysago "%Y" "$DAYSAGO" "$NOW")
 ZMONTHEND=$(datedaysago "%m" "$DAYSAGO" "$NOW")
 ZDAYEND=$(datedaysago "%d" "$DAYSAGO" "$NOW")
 MONTHEND=$(echo $ZMONTHEND | sed -e 's/^0//')
 DAYEND=$(echo $ZDAYEND | sed -e 's/^0//')

 YEARSTART=$(datedaysago "%Y" "$(($DAYSAGO + 6))" "$NOW")
 ZMONTHSTART=$(datedaysago "%m" "$(($DAYSAGO + 6))" "$NOW")
 ZDAYSTART=$(datedaysago "%d" "$(($DAYSAGO + 6))" "$NOW")
 MONTHSTART=$(echo $ZMONTHSTART | sed -e 's/^0//')
 DAYSTART=$(echo $ZDAYSTART | sed -e 's/^0//')

 {
  {
   if [ _"$MONTHSTART" != _"$MONTHEND" ]; then
    #### get last month's data
    Curl -s -k -b ${COOKIEJAR} \
        "${BASEURL}/calls.php?month=${MONTHSTART}&year=${YEARSTART}"
    #### go to index page before getting this month's data (?)
    gomainpage
   fi
   Curl -s -k -b ${COOKIEJAR} \
        "${BASEURL}/calls.php?month=${MONTHEND}&year=${YEAREND}"
  } \
  | sed -n -e \
 
's|[[:space:]]*<tr>.*\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).*\([0-9][0
-9]\):\([0-9]\{2\}\):\([0-9]\{2\}\).*FREE.*|\1 \2 \3 \4 \5 \6|p' \
  | while read YEAR MONTH DAY DURH DURM DURS; do
    if [ $YEAR$MONTH$DAY -ge $YEARSTART$ZMONTHSTART$ZDAYSTART -a
$YEAR$MONTH$DAY -le $YEAREND$ZMONTHEND$ZDAYEND ]; then
     echo "$DURH $DURM $DURS"
    fi
   done
 } | awk "$TOTALIZER"
 #### go to index page before logoff (?)
 gomainpage
;;
esac

LOGGEDIN=$(Curl -s -k -b ${COOKIEJAR} "${BASEURL}/index.php?part=logoff")

rm ${COOKIEJAR}

<----End

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2006-06-05