2013-07-05 47 views
0

任何人都可以提供以下幫助 我正在嘗試向RESTful API發出JSON請求。下面的代碼是好心由韋斯·弗朗共享發送無CURL的JSON HTTP查詢

代碼似乎能夠解碼,以JSON不錯,但作爲一個URL編碼字符串

<?php 
function rest_helper($url, $params = null, $verb = 'GET', $format = 'json') 
{ 
    $cparams = array(
    'http' => array(
     'method' => $verb, 
     'ignore_errors' => true 
    ) 
); 
    if ($params !== null) { 
    $params = http_build_query($params); 
    if ($verb == 'POST') { 
     $cparams['http']['content'] = $params; 
    } else { 
     $url .= '?' . $params; 
    } 
    } 

    $context = stream_context_create($cparams); 
    $fp = fopen($url, 'rb', false, $context); 
    if (!$fp) { 
    $res = false; 
    } else { 
    // If you're trying to troubleshoot problems, try uncommenting the 
    // next two lines; it will show you the HTTP response headers across 
    // all the redirects: 
    // $meta = stream_get_meta_data($fp); 
    // var_dump($meta['wrapper_data']); 
    $res = stream_get_contents($fp); 
    } 

    if ($res === false) { 
    throw new Exception("$verb $url failed: $php_errormsg"); 
    } 

    switch ($format) { 
    case 'json': 
     $r = json_decode($res); 
     if ($r === null) { 
     throw new Exception("failed to decode $res as json"); 
     } 
     return $r; 

    case 'xml': 
     $r = simplexml_load_string($res); 
     if ($r === null) { 
     throw new Exception("failed to decode $res as xml"); 
     } 
     return $r; 
    } 
    return $res; 
} 

我需要能夠發送:

  1. 添加應用程序/ JSON的內容類型
  2. 轉換PARAMS以JSON

不能用c網址在這種環境

最主要的是內容類型 - 目前默認爲url編碼 任何提示或想法表示讚賞 - 感謝


最新嘗試

function restHelper($url, $params = null, $verb = 'GET', $format = 'json'){ 
    $cparams = array(
    'http' => array(
     'method' => $verb, 
     'ignore_errors' => true, 
     'header' =>"Content-type: application/json \r\n" 
    ) 
); 
    if ($params !== 'None') { 
    $jparams = json_encode($params); 
    if ($verb == 'POST') { 
      $cparams['http']['content'] = $jparams; 

     } elseif ($verb =='PUT') { 
      $cparams['http']['content'] = $jparams; 
     } else { 
      $params = http_build_query($params); 
      $url .= '?' . $params; 
     } 
    } 

仍然沒有工作 - REST IDE的API測試很好似乎來自JSON的內容類型是如何工作的

+0

我寫[這](https://github.com/rdlowrey/Artax)正是因爲PHP的內置在HTTP檢索選項中很差。 – rdlowrey

+0

Briliant - 希望在將來使用它。當前的PHP環境是準系統,並且5.3.2 – prema

+0

那麼,它可以在精簡版PHP中正常工作 - 不需要任何擴展。但它確實需要5.4,所以我猜你沒有運氣。請注意5.3即將結束。你的主人應該升級...像昨天一樣。 – rdlowrey

回答

1

最後找到了一種包含CUR L在腳本中。 這裏是什麼工作(原型) (感謝羅娜珍http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl) 謝謝大家,看了這個

$service_url = 'http://dev.eventplus.co.nz/api/logon'; 
$ch = curl_init($service_url); 
$data = '[ 
    { 
     "Header": { 
      "Username": "[email protected]", 
      "SessionId": "123" 
     } 
    }, 
    { 
     "Config": {} 
    }, 
    { 
     "Params": {"Query": {"Password": "test12345"}} 
    } 
]'; 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data))                  
); 

curl_setopt($ch, CURLOPT_POSTFIELDS,$data); 
$response = curl_exec($ch); 
if ($response === false) { 
    $info = curl_getinfo($ch); 
    curl_close($ch); 
    die('error occured during curl exec. Additioanl info: ' . var_export($info)); 
} 
curl_close($ch); 
print $response;