使用Curl获取JSON对象

时间:2014-05-09 15:33:48

标签: php json curl httprequest

我知道如何使用httpRequest执行此操作:

    //create the httprequest object                
    $httpRequest_OBJ = new httpRequest($url, HTTP_METH_GET, $options);

    //add the content type
    $httpRequest_OBJ->setContentType = 'Content-Type: application/Json; charset=utf-8';
    //add the raw post data
    $httpRequest_OBJ->setRawPostData ($theData);
    //send the http request
    $result = $httpRequest_OBJ->send();

    try{
    $response = $result->getBody();
    }catch (HttpException $ex){

        echo $ex;


    }

什么是cURL等价物?

干杯。

3 个答案:

答案 0 :(得分:1)

这样的事情应该有效。

function apiCall($type, $params=array())
{
    //building up the url parameters
    $args=array();
    foreach($params as $field=>$value)
    {
        // Seperate each column out with it's corresponding value
        $args[]=$field.'='.str_replace(' ', '+',$value).'';
    }
    // Create the query
    $url = $this->baseurl.$type.'?'.implode('&',$args);
    //check if CURL is available, if so use curl to make the call, otherwise get the file content via file_get_contents()
    if (function_exists('curl_init'))
    {
        $ch = curl_init();
        // Set the URL
        curl_setopt($ch, CURLOPT_URL, $url);
        // Removes the headers from the output
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // Return the output instead of displaying it directly
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0');
        // Execute the curl session
        $result = curl_exec($ch);
        $errno = curl_errno($ch);
        // CURLE_SSL_CACERT || CURLE_SSL_CACERT_BADFILE
        if ($errno == 60 || $errno == 77) {
        self::errorLog('Invalid or no certificate authority found, '.
            'using bundled information');
        curl_setopt($ch, CURLOPT_CAINFO,
            dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ca_chain_bundle.crt');
        $result = curl_exec($ch);
        }
        // Close the curl session
        curl_close($ch);
        $res = json_decode($result, true);
        return $result;

    }
    else
    {
        // fallback if we don't have CURL or if ifs curently not available (whysoever)
        // A litte slower, but (usually) gets the job done
        $res =  file_get_contents($url);
        return json_decode($res, true);
    }
}

用法与此类似:

$res = $this->apiCall('search', array('term'=>$term, 'entity'=>$entity, 'country'=>$country));

这将返回一个关联数组。

答案 1 :(得分:1)

看看这个:

http://www.php.net/manual/en/curl.examples.php

示例:

<?php

// helper function
function curlGet($url)
{
    // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, $url);

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    // finished
    return $output;
}

// usage
var_dump(curlGet('http://google.com/'))

如果您要发出GET请求的服务器正在使用JSON字符串进行响应,您可以像这样对其进行解码:

$jsonStr = curlGet('http://pastebin.com/raw.php?i=S62Ar7mY');
$jsonObj = json_decode($jsonStr);

echo "<pre>";
print_r($jsonObj);

答案 2 :(得分:1)

您可以按照以下方式使用cURL

$ch = curl_init($url); // Put your URL here
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if($ispost==1){ // IF it is a post request
   curl_setopt($ch, CURLOPT_POST, 1);
}
else{
   curl_setopt($ch, CURLOPT_POST, 0);
}
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$output = curl_exec($ch);
curl_close($ch);        
print_r($output);