<p>You need the url, not the file path. Can you store the url, or the <code>http://localhost:8888</code> part of it in an environment variable? That gives you</p>
<pre><code>$url = getenv('CRAFT_URL') . $id . &quot;.json&quot;;
</code></pre>
<p>If it is not already in an environment variable, then in a <code>.env</code> file? <code>web/index.php</code> that comes with Craft is a good example of loading a <code>.env</code> file, and then using the environment variable.</p>

CC BY-SA 4.0
2020-07-22T22:23:50.833Z
Thanks for the answer
mitrol
CC BY-SA 4.0
2020-07-20T18:57:13.740Z
2020-07-20T18:57:13.740Z
Marion Newlevant
<p>I need a few small bits of data from Craft in a PHP file. It’s a really small task, so I don’t want to delve into writing plugins or anything hardcore.</p>
<p>It seemed to me the simplest way to approach it would be to use the ElementAPI plugin and then read the resulting JSON in the PHP (this is being tested using MAMP):</p>
<pre><code>&lt;?php

// ID of entry I want
$id = 563;

// Grab data from ElementAPI
$url = &quot;http://localhost:8888/myApi/&quot; . $id . &quot;.json&quot;;
$data = file_get_contents($url);
$json = json_decode($data);

// output data
echo &quot;Entry title: &quot; . $json -&gt; title;

?&gt;
</code></pre>
<p>This works perfectly.</p>
<p>However, that hard-coded URL does not sit well with me.</p>
<p>I tried grabbing the directory using a few variations, eg:</p>
<pre><code>$url =  __DIR__ . &quot;/myApi/&quot; . $id . &quot;.json&quot;;
</code></pre>
<p>That gave me the correct path to the file, but the JSON isn’t generated — presumably because without the hard-coded address it’s not running the file through MAMP, so the PHP isn’t running.</p>
<p>I surely cannot manually update the URL every time I deploy a revision — that seems like a disaster in the making.</p>
<p>TIA for any solutions.</p>

CC BY-SA 4.0
2020-07-18T21:00:12.730Z
What "small bits of data" are you asking for? What are you using that data for? Depending on what you're doing, you can also hook into Craft via a shell script but it would help to clarify your use case.
RitterKnight
CC BY-SA 4.0
2020-07-19T17:10:41.647Z
I need to grab 1–3 products, and the title, primary image, and a list of options for each, and I need to do it in a PHP file due to other aspects of the project. In twig it would be no problem, and the above _is_ working, I’m just baffled as to how to make the $url in the script flexible enough that I don’t need to hardcode it.
mitrol
<php><plugin-elementapi>
Setting relative path to ElementAPI in PHP
# Setting relative path to ElementAPI in PHP

(Asked by: username_0 on 2020-07-17)

I need a few small bits of data from Craft in a PHP file. It’s a really small task, so I don’t want to delve into writing plugins or anything hardcore.
It seemed to me the simplest way to approach it would be to use the ElementAPI plugin and then read the resulting JSON in the PHP (this is being tested using MAMP):
 title;

?>

This works perfectly.
However, that hard-coded URL does not sit well with me.
I tried grabbing the directory using a few variations, eg:
$url =  __DIR__ . "/myApi/" . $id . ".json";

That gave me the correct path to the file, but the JSON isn’t generated — presumably because without the hard-coded address it’s not running the file through MAMP, so the PHP isn’t running.
I surely cannot manually update the URL every time I deploy a revision — that seems like a disaster in the making.
TIA for any solutions.

**Tags:** php, plugin-elementapi
> **Comments:**
> **username_1**: What "small bits of data" are you asking for? What are you using that data for? Depending on what you're doing, you can also hook into Craft via a shell script but it would help to clarify your use case.  
> **username_0**: I need to grab 1–3 products, and the title, primary image, and a list of options for each, and I need to do it in a PHP file due to other aspects of the project. In twig it would be no problem, and the above _is_ working, I’m just baffled as to how to make the $url in the script flexible enough that I don’t need to hardcode it.  

---

## 1 answers

### Answer by username_2 on 2020-07-20. Score: 1 | Accepted answer

You need the url, not the file path. Can you store the url, or the http://localhost:8888 part of it in an environment variable? That gives you
$url = getenv('CRAFT_URL') . $id . ".json";

If it is not already in an environment variable, then in a .env file? web/index.php that comes with Craft is a good example of loading a .env file, and then using the environment variable.

> **Comments:**
> **username_0**: Thanks for the answer  

---

