2014-05-09 25 views
0

我知道如何使用的HttpRequest做到這一點:使用捲曲得到一個JSON對象

//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; 


    } 

什麼是捲曲等同?

乾杯。

回答

1

您可以通過以下方式

$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); 
+0

curl_setopt($ ch,CURLOPT_POST,0);所以這意味着GET ?,我覺得奇怪,你不能明確寫入GET – bobo2000

+0

這是正確的朋友。對於GET請求,它是0和1的POST。其實默認情況下它是GET。 – Janaki

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)); 

這將返回一個關聯數組。

+0

籲......似乎是一個超必殺使用捲曲。 OP正試圖做出一個簡單的GET請求。 – Latheesan

+0

好吧,從我目前的項目中,我必須解析蘋果搜索/查找api和亞馬遜affilate api的結果。 –

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);