2

I need to call a service to retrieve the available values for a select in a form I am creating. Therefore, I need to do this in the .module file and load the options with an array of items that I received from the service.

I retrieve that results but due to it having a callback function, I cannot seem to iterate through the data to retrieve only certain values from the data.

I have the following.

$request = drupal_http_request('http://[local]/TemplateManager/GetRenderingStrategies?Wizard=false&callback=jsonCallback');

I get back this.

jsonCallback([
{
    "HTMLRenderingStrategyId": 1,
    "IsEnabled": true,
    "CreatedDate": "2013-04-19T10:30:36.563",
    "CreatedBy": 1,
    "UpdatedDate": "2013-04-19T10:30:36.563",
    "UpdatedBy": 1,
    "RowGuid": "19223fcf-45c2-452b-aac6-ba357169ff26",
    "Name": "DEFAULT",
    "FormTemplateView": "/Templates/Default/FormTemplate.cshtml",
    "ManagedChoiceTemplateView": null,
    "NoMatchTemplateView": null,
    "ThankYouTemplateView": null,
    "NoThankYouTemplateView": null,
    "CSSPath": "/Templates/Default/css/Base.css",
    "CrossSellTemplateView": null,
    "CrossSellMaxProgramsToDisplay": null
},
{
    "HTMLRenderingStrategyId": 2,
    "IsEnabled": true,
    "CreatedDate": "2013-04-19T10:30:36.563",
    "CreatedBy": 1,
    "UpdatedDate": "2013-04-19T10:30:36.563",
    "UpdatedBy": 1,
    "RowGuid": "2ea1ecb7-0281-4361-87ee-d4cfe12c9b0c",
    "Name": "EMD",
    "FormTemplateView": "/Templates/EMD/FormTemplate.cshtml",
    "ManagedChoiceTemplateView": null,
    "NoMatchTemplateView": null,
    "ThankYouTemplateView": null,
    "NoThankYouTemplateView": null,
    "CSSPath": "/Templates/EMD/css/Base.css",
    "CrossSellTemplateView": null,
    "CrossSellMaxProgramsToDisplay": null
},
{
    "HTMLRenderingStrategyId": 3,
    "IsEnabled": true,
    "CreatedDate": "2013-04-19T10:30:36.563",
    "CreatedBy": 1,
    "UpdatedDate": "2013-04-19T10:30:36.563",
    "UpdatedBy": 1,
    "RowGuid": "e2d7ed2e-7ed7-4d2e-aecb-0a589c7ad338",
    "Name": "ELEARNERS",
    "FormTemplateView": "/Templates/eLearners/FormTemplate.cshtml",
    "ManagedChoiceTemplateView": null,
    "NoMatchTemplateView": null,
    "ThankYouTemplateView": null,
    "NoThankYouTemplateView": null,
    "CSSPath": "/Templates/eLearners/css/Base.css",
    "CrossSellTemplateView": "/Templates/eLearners/CrossSellTemplate.cshtml",
    "CrossSellMaxProgramsToDisplay": 10
}]);

What is my next step to pull out only the "Name" from the result set?

I am at a loss here. I tried looking for the answer and it seems all the jsonp calls are done in javascript/jQuery and that doesn't really suit me.

If you feel I am doing this incorrectly, please let me know. I have never worked with jsonp or json and Drupal for that matter.

3
  • you want to get Name from the result set using php or javascript/jQuery?
    – Aboodred1
    May 3, 2013 at 15:33
  • First off, thank you for answering my question. I want to use php. For some reason i cannot access the data.
    – Alex
    May 3, 2013 at 15:39
  • question: why use jsonp with php? just use json
    – Aboodred1
    May 3, 2013 at 16:13

1 Answer 1

2

You can create a custom function called jsonp_decode with one argument to convert JSONP format to JSON format and return decoded JSON

function jsonp_decode($jsonp) {
  $jsonp = preg_replace('/^.*?\(/', "", $jsonp);
  $jsonp = preg_replace('/\);\s*$/', "", $jsonp);
  return drupal_json_decode($jsonp);
}

Then you can pull the "Name" from the result set as the following code:

$request = drupal_http_request('http://[local]/TemplateManager/GetRenderingStrategies?Wizard=false&callback=jsonCallback');

$names = array();

$items = jsonp_decode($request);

foreach($items as $item) {
  $names[] = $item['Name'];
}

var_dump($names);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.