RapidAPI: php and json code

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1661 Views - Last Post: 05 April 2019 - 11:30 AM Rate Topic: -----

#1 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

RapidAPI: php and json code

Posted 31 March 2019 - 08:16 AM

I have two problems I would like to solve regrading RadpidAPI and PHP.

My first problem is as followed - when I enter the API URL into my browser I receive the error "Missing RapidAPI application key." Moreover, I understand I need my API Key but when I try to concatenate the URL extension it won't run? Here is an example of another URL and API Key I have managed to work corretly: https://www.alphavan...875V9AAHD7FW%22

The second problem is my code and outputting the json file.

 

<?php

$API_KEY = "xxxxx";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,("https://dev132-cricket-live-scores-v1.p.rapidapi.com/matchseries.php?seriesid=2141" . $API_KEY));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
$result = json_decode($server_output);

$data_cricket = $result->{'completedMatchCount'};
$dataForMatches = $data_cricket->{'matchList'};

echo $dataForMatches->{'matches'} . '<br/>;

?>




json file.

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: RapidAPI: php and json code

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: RapidAPI: php and json code

Posted 31 March 2019 - 08:43 AM

Is it correct for the api key to be added directly to the seriesid without separator?
Was This Post Helpful? 0
  • +
  • -

#3 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 31 March 2019 - 08:50 AM

I've tried various combinations of concatenation but nothing seems to work?
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: RapidAPI: php and json code

Posted 31 March 2019 - 09:05 AM

As Andrew is hinting, aren't you missing the & between the parameters in the query string?
Was This Post Helpful? 0
  • +
  • -

#5 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 31 March 2019 - 09:12 AM

I did add the & but all I get is an error?

Example:

https://dev132-crick...jsn88a2caf33cd5
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: RapidAPI: php and json code

Posted 31 March 2019 - 09:19 AM

You seem to be using the parameter name X-RapidAPI-Key, which looks more like a request header. In the URL that worked in the first post, the query parameter seemed to be called apikey. I don't know why you changed that.
Was This Post Helpful? 0
  • +
  • -

#7 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 31 March 2019 - 09:25 AM

The top URL is from a different website, the bottom URL is from RapidAPI and says it should use X-RapidAPI-Key?

$response = Unirest\Request::get("https://dev132-cricket-live-scores-v1.p.rapidapi.com/matchseries.php?seriesid=2141",
  array(
    "X-RapidAPI-Key" => "1479c0acebmsh8f4278f7c091344p15973fjsn88a2caf33cd5"
  )
);



Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: RapidAPI: php and json code

Posted 31 March 2019 - 09:34 AM

Oh, you're right - I misread. Is that sample of code sending the value in the headers now then? Does it work? Obviously check the docs for the API to see whether it is indeed a request header (names like that conventionally are, but worth checking anyway!).

FWIW, I made a request supplying the X-RapidAPI-Key header and get a 403, rather than a 401, so, yeah, it looks like the header is correct.
Was This Post Helpful? 0
  • +
  • -

#9 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 03 April 2019 - 09:18 AM

I have managed to concatenate the API Key to the URL but I am still having trouble with the output of the code. My code outputs the error 'Trying to get property of non-object'. The second piece of code is the non-working code.

Example of my working output:
},

"Time Series (Daily)": {	
"2019-04-03:"{
"1. open" "119.8600",
"2. high" "120.1400",
"3. low	" "119.8600",
"4. close" "120.1000",
"5. volume" "836799"
},

$dataForAllDays = $result->{'Time Series (Daily)'};
$dataForSingleDate = $dataForAllDays->{'2019-03-29'};

echo $dataForMatches->{'1. open'} . '<br/>;
echo $dataForMatches->{'2. high'} . '<br/>;
echo $dataForMatches->{'3. low'} . '<br/>;



Example of non-working code:

},
"matchList": {
"matches": [
{
"id": 43470,
"matchTypeId":0,
"statisticsProvider":"Opta Sports",
"series": {
"id": 2141,
"name":	"IPL 2018",
"shortName": "IPL 2018",
"shieldImageUrl":"https://www.cricket.com.au/-/media/Logos/Series/2016/IPL-new.ashx"
},

$data_cricket = $result->{'matchList)'};
$dataForMatches = $data_cricket->{'matches'};

echo $dataForMatches->{'id'} . '<br/>;
echo $dataForMatches->{'matchTypeId'} . '<br/>;
echo $dataForMatches->{'statisticsProvider'} . '<br/>;


Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: RapidAPI: php and json code

Posted 03 April 2019 - 09:57 AM

What are the full error details? What line does it refer to?
Was This Post Helpful? 0
  • +
  • -

#11 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 03 April 2019 - 10:04 AM

The errors are:

Notice: Trying to get property of non-object in xxx on line 18
Notice: Trying to get property of non-object in xxx on line 19
Notice: Trying to get property of non-object in xxx on line 20

The part below:
echo $dataForMatches->{'id'} . '<br/>'; 
echo $dataForMatches->{'matchTypeId'} . '<br/>';
echo $dataForMatches->{'statisticsProvider'} . '<br/>';


Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2287
  • View blog
  • Posts: 6,978
  • Joined: 15-January 14

Re: RapidAPI: php and json code

Posted 03 April 2019 - 10:12 AM

Typo here:

$result->{'matchList)'}
Was This Post Helpful? 0
  • +
  • -

#13 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 03 April 2019 - 10:16 AM

That's just me reprinting the code - I checked my computer code there is no typo.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: RapidAPI: php and json code

Posted 03 April 2019 - 10:20 AM

Please copy and paste the true code, don't attempt to retype it.
Was This Post Helpful? 0
  • +
  • -

#15 jacob_1988   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 18

Re: RapidAPI: php and json code

Posted 03 April 2019 - 10:27 AM

$API_KEY = "1479c0acebmsh8f4278f7c091344p15973fjsn88a2caf33cd5";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,("https://dev132-cricket-live-scores-v1.p.rapidapi.com/matchseries.php?seriesid=2141&rapidapi-key=" . $API_KEY));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$result = json_decode($server_output);

$data_cricket = $result->{'matchList'};
$dataForMatches = $data_cricket->{'matches'};


echo $dataForMatches->{'id'} . '<br/>';
echo $dataForMatches->{'matchTypeId'} . '<br/>';
echo $dataForMatches->{'statisticsProvider'} . '<br/>';



This post has been edited by CTphpnwb: 04 April 2019 - 04:42 PM
Reason for edit:: Fixed code tag

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2