0

I'm trying to combine data from 2 API calls. The first call gets most the data I need, the second call that's made uses an item(bill_id) from the first call as a parameter in order to get data for the second API call. I only need 1 item from the second call so I'm trying to add it to the first array and then pass that array as json to the client side.

Here's the webmethod controller..

    //AJAX calls
    function getUpcomingBills ($params = null) {
        $upcomingBills = $this->CongressAPI->get('upcoming_bills', $params);
        $upcomingBills = json_decode($upcomingBills, true);


        foreach($upcomingBills as $bill) {
            $p = ['bill_id' => $bill['bill_id']];
            $title = $this->CongressAPI->get('bills', $p);

            $bill['title'] = $title[17];
        }

        echo json_encode($upcomingBills);
    }

And the model...

    public function get($method = null, $params = null) {

        $key = "...";
        $url = "http://congress.api.sunlightfoundation.com/";
        $params['apikey'] = $key;

        $ret = $this->curl->simple_get($url.$method, $params);

        if (!empty($ret)) {
            $ret = json_decode($ret, true);
            $ret = json_encode($ret["results"]);                
        } else {
            $ret = '';
        }

        return $ret;
    }

This is returning a JSON object without error, however it's not appending the "Title" to the first Object.

Also fyi, when I try and use the title of the associative array of the second call (instead of $title[17]), I get the error:

Message: Illegal string offset 'short_title'

2nd API call returns the following: (Trying to get 'short_title')

{
    "results": [
        {
            "bill_id": "s754-114",
            "bill_type": "s",
            "chamber": "senate",
            "committee_ids": [
                "SLIN"
            ],
            "congress": 114,
            "cosponsors_count": 0,
            "enacted_as": null,
            "history": {
                "active": true,
                "active_at": "2015-04-15",
                "awaiting_signature": false,
                "enacted": false,
                "vetoed": false
            },
            "introduced_on": "2015-03-17",
            "last_action_at": "2015-08-05",
            "last_version": {
                "version_code": "pcs",
                "issued_on": "2015-03-17",
                "version_name": "Placed on Calendar Senate",
                "bill_version_id": "s754-114-pcs",
                "urls": {
                    "html": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/html/BILLS-114s754pcs.htm",
                    "pdf": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/pdf/BILLS-114s754pcs.pdf",
                    "xml": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/xml/BILLS-114s754pcs.xml"
                },
                "pages": 54
            },
            "last_version_on": "2015-03-17",
            "last_vote_at": null,
            "number": 754,
            "official_title": "An original bill to improve cybersecurity in the United States through enhanced sharing of information about cybersecurity threats, and for other purposes.",
            "popular_title": null,
            "related_bill_ids": [
                "hr1560-114",
                "hr1731-114"
            ],
            "short_title": "Cybersecurity Information Sharing Act of 2015",
            "sponsor": {
                "first_name": "Richard",
                "last_name": "Burr",
                "middle_name": "M.",
                "name_suffix": null,
                "nickname": null,
                "title": "Sen"
            },
            "sponsor_id": "B001135",
            "urls": {
                "congress": "http://beta.congress.gov/bill/114th/senate-bill/754",
                "govtrack": "https://www.govtrack.us/congress/bills/114/s754",
                "opencongress": "https://www.opencongress.org/bill/s754-114"
            },
            "withdrawn_cosponsors_count": 0
        }
    ],
    "count": 1,
    "page": {
        "count": 1,
        "per_page": 20,
        "page": 1
    }
}

Please let me know if there's anything unclear or if you need more info.

dave
  • 215
  • 3
  • 12
  • 1
    I think you should look at the answer to this question: http://stackoverflow.com/questions/15472033/how-to-update-specific-keys-value-in-an-associative-array-in-php – Katrina Aug 06 '15 at 19:59
  • Thanks for the reference, it helped me figure it out – dave Aug 06 '15 at 22:19

1 Answers1

0

Thanks to the reference by GreeKatrina, I got it working by changing my method to the following.

function getUpcomingBills ($params = null) {
            $upcomingBills = $this->CongressAPI->get('upcoming_bills', $params);
            $upcomingBills = json_decode($upcomingBills, true);


            foreach($upcomingBills as $bill => $val) {
                $p = ['bill_id' => $upcomingBills[$bill]['bill_id']];
                $title = json_decode($this->CongressAPI->get('bills', $p), true);
                $upcomingBills[$bill]['title'] = $title;
            }

            echo json_encode($upcomingBills);
        }

It now passes the who json array within the object

dave
  • 215
  • 3
  • 12