Reputation: 5053
I'm working with Curl to make some petitions to a php script, I'm trying to make the petitions as you see below, my script is ajax2.php
$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36,'method'=>'prueba'];
$defaults = array(
CURLOPT_URL => getcwd().'\src\myApp\ajax2.php',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params),
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
curl_exec($ch);
if (curl_errno($ch)) {
// this would be your first hint that something went wrong
die('Couldn\'t send request: ' . curl_error($ch));
}
but I get this error: Couldn't send request: Could not resolve host: C
so, how should I call a script that is inside my project folder?
Upvotes: 1
Views: 4110
Reputation: 3758
curl or libcurl is, as they put it on the official site, an "URL transfer library", i.e. it expects to work on URL targets. However, you are passing it a file path like C:\PathToYourStuff\src\myApp\ajax2.php
, which is not a valid URL format. That is why the error message says
Could not resolve host: C
Interpreting the path above as an URL would mean that C is the host name, because the colon (":") is the part which separates the hostname from the port in an URL. (The part behind that is then nonsense, from the viewpoint of an URL parser, but it does not even get that far, because the supposed host name cannot be resolved.)
So what you have to use instead is an URL that points to that file, e.g. something like http://localhost/path-to-your-stuff/src/myApp/ajax2.php
.
So change your code to something like this and adjust the URL as needed:
$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36,'method'=>'prueba'];
$defaults = array(
CURLOPT_URL => 'http://localhost/path-to-your-stuff/src/myApp/ajax2.php',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params),
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
curl_exec($ch);
// ... and so on, as seen in your question
Upvotes: 3