简体   繁体   中英

PHP curl get api response

I'm trying to get a json object from an API and it's returning a string. I tried to json_decode it but it's all messed up. I can't post the data because it's sensitive but here is an example like it.

{"status":"ok", "list_i_want":[{param1:"blah",param2:"blah",param3:"blah"},{...}]}

I tried to just cut out the first part and the last } but it still won't decode into a json object. It could also be my curl_setopt()'s.

$curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Accept: application/json'
    ));


    $resp = curl_exec($curl);
    curl_close($curl);
    DD($resp);   // laravel's die and dump

For CURL get API with custom header.

 $subscription_key  ='';
    $host = '';    
    $request_headers = array(
                    "X-Mashape-Key:" . $subscription_key,
                    "X-Mashape-Host:" . $host
                );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

    $season_data = curl_exec($ch);

    if (curl_errno($ch)) {
        print "Error: " . curl_error($ch);
        exit();
    }

    // Show me the result
    curl_close($ch);
    $json= json_decode($season_data, true);
    dd($json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM