1

I have some code which calls a 3rd party service and returns stock/etf data. This is the error:

    PHP Warning:  file_get_contents(http://********.******.********.com/*************.json?apikey=********************************&symbols=FFHG,FFTI,NUDM,NUEM,GIGB,AZIA,BCHP,BRAF,BRXX,BSCF,BSJF,DBIZ,EEHB,EWRM,EWRS,GURX,INC,ITF,JUNR,LAG,MATL,MDLL,SUBD,TENZ,CU,MULT,PLTM,HYLB,SGQI,WTID,WTIU,CUMB,COMB,COMG,BVAL,PPLN,ICOW,CALF,GMFL,RNDM,RNEM,RNLC,RNMC,RNSC,RNDV,FCAL,VSMV,OCIO,TTAI,GOAU,VESH,USMF,GSSC,SMMD,IBD,PREF,FANZ,FNG,EUXL,SUSC,SUSB,IGEB,HYDB,SQLV,REEM,RGLB,REFA,SPMV,USEQ,EQRR,EMXC,USOU,USOD,DMRL,DMRM,DMRS,DMRI,EDOW,AMCA,ULBR,DLBR,EMBU,DWPP,FMDG,FFIU,FPEI,YESR,FLMI,FLMB,MFUS,MFEM,MFDX,SECT,MAGA,GHYB,OBOR,PFFD,MLQD,LLQD,IBDS,UBRT,DBRT,RBUS,RBIN,MXDU,XNTK,GSEW,PBUS,PBSM,PBDM,PBEE,PBTP,BSCR,BSJP,HTRB,DALT,PBND,NUBD,LFEQ,SCHK,CHGX,USMC,KEMQ,VGFO,DIAL,GOP,DEMS,PLCY,BERN,BRGL,SPMD,AIEQ,KGRN,MMIT,MMIN,PVAL,PMOM,LOGO,GRMY,EURZ,USHY,ULVM,USVM,UIVM,UEVM,USTB,UITB,CEY,FLAU,FLCA,FLEE,FLEH,FLFR,FLGR,FLHK,FLIY,FLIP,FLJH,FLGB,FLKR,FLBR,FLCH,FLMX,FLTW,GUDB,BIBL,RVRS,FTVA,FFSG,FFTG,FMHI,SDVY,JDIV,JMIN,JV
 AL,JQUA,JMOM,ENTR,JHSC,PXUS,VTC,OMFL,OMF in ..../functions.php on line 931

It breaks after "JV", but the actual value is JVAL (you'll see the AL bit on the next line).

I have checked the database and the value that should 100% be returned is JVAL. There are no spaces in the field.

I am not sure why it is breaking at that point.

Here is the PHP function which calls the file_get_contents:

function get_etf_data($syms) {

  if (is_array($syms)) {
    $syms = implode(",", $syms);
  }
  $url = 'http://********.******.********.com/*************.json?apikey=********************************&symbols='.$syms;
  //die($url); // debug
  sn_bc_counter('get_etf_data',$_SERVER['REQUEST_URI']);
  $data = file_get_contents($url,"r");
  $data = json_decode($data);
  $arr = array();

  foreach ($data->results as $result) {
    $arr[] = $result;
  }

  return $arr;
}

Any ideas to point me in the right direction?

youslippin
  • 21
  • 4
  • 1
    why are you using file_get_contents to fetch data from another server? it would be better if you can use curl – Shashank Shekhar Jul 08 '20 at 18:39
  • 1
    Is this really a native PHP error, because it's not documentated and I cant reproduce it. Another note: You are using insecure http in the request. – Daniel W. Jul 08 '20 at 18:47
  • @ShashankShekhar why? `file_get_contents()` is made to request URIs , why he shouldn't use it? Unreasoned advice there. – Daniel W. Jul 08 '20 at 18:48
  • 1
    @DanielW. yes i know it is used. but when the url is long or if there is 404 it does not give proper error and breaks the page. So i prefer curl as it is faster, handles error in better way and supports more request type other than get. – Shashank Shekhar Jul 08 '20 at 18:56
  • Thanks! I went with the CURL method and it seems to be working – youslippin Jul 08 '20 at 19:38
  • @ShashankShekhar you can get both with `file_get_contents()`: [Proper response code](https://stackoverflow.com/a/15620189/1948292) and [use other methods than GET](https://stackoverflow.com/a/2445332/1948292) (note: all errors unhandled properly might break a page...) – Daniel W. Jul 08 '20 at 22:13

1 Answers1

-1

Can you maybe check with curl instead of file_get_contents?

Like so:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseBody = curl_exec($ch);

if ($responseBody === false) {
    echo "CURL Error: " . curl_error($ch);
}

$data = json_decode($responseBody);