including php file with json mime type without changing the mime type of the page including it2019 Community Moderator Electionsend http request with CURL to local fileWhat is the correct JSON content type?Parsing values from a JSON file?Returning JSON from a PHP ScriptHow much bandwidth do I generate to destination web site with my curl code?How do I write JSON data to a file?php Curl posting to PHPBBHow to implement cache system in php for json apiHow to load details automaticallycURL not working sometimes and gives empty resulthow can i check if RESTAPI is down using curl php
Why one should not leave fingerprints on bulbs and plugs?
What is the rarity of this homebrew magic staff?
Gravity magic - How does it work?
Use void Apex method in Lightning Web Component
Property of summation
Sailing the cryptic seas
What approach do we need to follow for projects without a test environment?
How Could an Airship Be Repaired Mid-Flight
What is the significance behind "40 days" that often appears in the Bible?
Interplanetary conflict, some disease destroys the ability to understand or appreciate music
Are all passive ability checks floors for active ability checks?
Why doesn't using two cd commands in bash script execute the second command?
Recruiter wants very extensive technical details about all of my previous work
How to create the Curved texte?
Charles Hockett - 'F' article?
In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf
A limit with limit zero everywhere must be zero somewhere
Use of undefined constant bloginfo
How to read the value of this capacitor?
What are substitutions for coconut in curry?
how to draw discrete time diagram in tikz
Why doesn't the EU now just force the UK to choose between referendum and no-deal?
What should tie a collection of short-stories together?
Is a party consisting of only a bard, a cleric, and a warlock functional long-term?
including php file with json mime type without changing the mime type of the page including it
2019 Community Moderator Electionsend http request with CURL to local fileWhat is the correct JSON content type?Parsing values from a JSON file?Returning JSON from a PHP ScriptHow much bandwidth do I generate to destination web site with my curl code?How do I write JSON data to a file?php Curl posting to PHPBBHow to implement cache system in php for json apiHow to load details automaticallycURL not working sometimes and gives empty resulthow can i check if RESTAPI is down using curl php
In a PHP script, I want to grab some json data that is being generated dynamically by another PHP script.
For an example, let's use this simple example of a dynamic PHP-generated JSON file:
<?php
// note: unlike this simplified example, the data in my script is dynamically generated
$some_data = array(
'color' => 'blue',
'number' => 33,
'length' => 'long'
);
header('Content-Type: application/json');
echo json_encode($some_data);
...which would render this json file:
"color": "blue", "number": 33, "length": "long"
Now how do I include this PHP file from another PHP file so I can get that data dynamically?
If I use $data = include 'json_data.php';, it doesn't store the data in my $data var, it takes the mime type header being defined in that include and determing the page is that mime type instead of determining the data from the include is that mime type and separate.
file_get_contents() and fopen() are only returning the actual PHP code, not rendering it and returning the resulting json data.
I could use cURL...
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://www.example.com/json_data.php');
header('Content-Type: application/json');
echo curl_exec($c);
curl_close($c);
...but doesn't that seem overkill for accessing a local file? Plus, that will take some extra setup work in my local vagrant dev environment to make my local file HTTP-accessible for cURL to be able to access it.
php json
add a comment |
In a PHP script, I want to grab some json data that is being generated dynamically by another PHP script.
For an example, let's use this simple example of a dynamic PHP-generated JSON file:
<?php
// note: unlike this simplified example, the data in my script is dynamically generated
$some_data = array(
'color' => 'blue',
'number' => 33,
'length' => 'long'
);
header('Content-Type: application/json');
echo json_encode($some_data);
...which would render this json file:
"color": "blue", "number": 33, "length": "long"
Now how do I include this PHP file from another PHP file so I can get that data dynamically?
If I use $data = include 'json_data.php';, it doesn't store the data in my $data var, it takes the mime type header being defined in that include and determing the page is that mime type instead of determining the data from the include is that mime type and separate.
file_get_contents() and fopen() are only returning the actual PHP code, not rendering it and returning the resulting json data.
I could use cURL...
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://www.example.com/json_data.php');
header('Content-Type: application/json');
echo curl_exec($c);
curl_close($c);
...but doesn't that seem overkill for accessing a local file? Plus, that will take some extra setup work in my local vagrant dev environment to make my local file HTTP-accessible for cURL to be able to access it.
php json
add a comment |
In a PHP script, I want to grab some json data that is being generated dynamically by another PHP script.
For an example, let's use this simple example of a dynamic PHP-generated JSON file:
<?php
// note: unlike this simplified example, the data in my script is dynamically generated
$some_data = array(
'color' => 'blue',
'number' => 33,
'length' => 'long'
);
header('Content-Type: application/json');
echo json_encode($some_data);
...which would render this json file:
"color": "blue", "number": 33, "length": "long"
Now how do I include this PHP file from another PHP file so I can get that data dynamically?
If I use $data = include 'json_data.php';, it doesn't store the data in my $data var, it takes the mime type header being defined in that include and determing the page is that mime type instead of determining the data from the include is that mime type and separate.
file_get_contents() and fopen() are only returning the actual PHP code, not rendering it and returning the resulting json data.
I could use cURL...
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://www.example.com/json_data.php');
header('Content-Type: application/json');
echo curl_exec($c);
curl_close($c);
...but doesn't that seem overkill for accessing a local file? Plus, that will take some extra setup work in my local vagrant dev environment to make my local file HTTP-accessible for cURL to be able to access it.
php json
In a PHP script, I want to grab some json data that is being generated dynamically by another PHP script.
For an example, let's use this simple example of a dynamic PHP-generated JSON file:
<?php
// note: unlike this simplified example, the data in my script is dynamically generated
$some_data = array(
'color' => 'blue',
'number' => 33,
'length' => 'long'
);
header('Content-Type: application/json');
echo json_encode($some_data);
...which would render this json file:
"color": "blue", "number": 33, "length": "long"
Now how do I include this PHP file from another PHP file so I can get that data dynamically?
If I use $data = include 'json_data.php';, it doesn't store the data in my $data var, it takes the mime type header being defined in that include and determing the page is that mime type instead of determining the data from the include is that mime type and separate.
file_get_contents() and fopen() are only returning the actual PHP code, not rendering it and returning the resulting json data.
I could use cURL...
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://www.example.com/json_data.php');
header('Content-Type: application/json');
echo curl_exec($c);
curl_close($c);
...but doesn't that seem overkill for accessing a local file? Plus, that will take some extra setup work in my local vagrant dev environment to make my local file HTTP-accessible for cURL to be able to access it.
php json
php json
asked Mar 6 at 19:45
Talk Nerdy To MeTalk Nerdy To Me
8910
8910
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Why don't you create a function that handles the logic of returning the JSON (or the array used to create the JSON) and use it in both scripts, so you don't have to include the first PHP file, just the file that contains your shared function.
Here's what I mean:
<?php
function getJson()
// Your real logic, not static content
return [
'color' => 'blue',
'number' => 33,
'length' => 'long'
];
If you have that function above in its own PHP file, it can be included (or required) in both PHP scripts you are writing.
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031074%2fincluding-php-file-with-json-mime-type-without-changing-the-mime-type-of-the-pag%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why don't you create a function that handles the logic of returning the JSON (or the array used to create the JSON) and use it in both scripts, so you don't have to include the first PHP file, just the file that contains your shared function.
Here's what I mean:
<?php
function getJson()
// Your real logic, not static content
return [
'color' => 'blue',
'number' => 33,
'length' => 'long'
];
If you have that function above in its own PHP file, it can be included (or required) in both PHP scripts you are writing.
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
add a comment |
Why don't you create a function that handles the logic of returning the JSON (or the array used to create the JSON) and use it in both scripts, so you don't have to include the first PHP file, just the file that contains your shared function.
Here's what I mean:
<?php
function getJson()
// Your real logic, not static content
return [
'color' => 'blue',
'number' => 33,
'length' => 'long'
];
If you have that function above in its own PHP file, it can be included (or required) in both PHP scripts you are writing.
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
add a comment |
Why don't you create a function that handles the logic of returning the JSON (or the array used to create the JSON) and use it in both scripts, so you don't have to include the first PHP file, just the file that contains your shared function.
Here's what I mean:
<?php
function getJson()
// Your real logic, not static content
return [
'color' => 'blue',
'number' => 33,
'length' => 'long'
];
If you have that function above in its own PHP file, it can be included (or required) in both PHP scripts you are writing.
Why don't you create a function that handles the logic of returning the JSON (or the array used to create the JSON) and use it in both scripts, so you don't have to include the first PHP file, just the file that contains your shared function.
Here's what I mean:
<?php
function getJson()
// Your real logic, not static content
return [
'color' => 'blue',
'number' => 33,
'length' => 'long'
];
If you have that function above in its own PHP file, it can be included (or required) in both PHP scripts you are writing.
answered Mar 6 at 19:56
MattMatt
93
93
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
add a comment |
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
That line of logic sure could be an alternative solution.
– Talk Nerdy To Me
Mar 6 at 20:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031074%2fincluding-php-file-with-json-mime-type-without-changing-the-mime-type-of-the-pag%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown