Off-resonant interaction of fluctuating photons in a resonator with a qubit increases the qubit dephasing rate. We use this effect to measure a small average number of intracavity photons that are coherently or thermally driven. For spectral resolution, we do this by subjecting the qubit to a Carr-Purcell-Meiboom-Gill sequence and record the qubit dephasing rate for various periods between qubit π pulses. The recorded data is then analyzed with formulas for the photon-induced dephasing rate derived for the non-Gaussian noise regime with an arbitrary ratio of the resonator dispersive shift to decay rate. We show that the presented Carr-Purcell-Meiboom-Gill dephasing rate formulas agree well with experimental results and demonstrate measurement of thermal and coherent photon populations at the level of a few 10^{-4}.