2015-11-25 89 views
0

我与卷曲的工作,使一些上访PHP脚本,我想为你看到下面让上访解析主机,我的剧本是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)); 

} 

但我得到这个错误:Couldn't send request: Could not resolve host: C所以,我应该如何调用我的项目文件夹内的脚本?

+0

Linux使用/不是\ – Mihai

+3

你应该使用HTTP或HTTPS URL。如果你在localhost,那么'CURLOPT_URL =>'http:// localhost/YOUT_APP_DIR/src/myApp/ajax2.php',' – jibon57

+0

@ jibon57谢谢你!工作! –

回答

0

curl或libcurl正如它们在official site上所指出的那样是“URL传输库”,即它期望在URL目标上工作。但是,您传递的文件路径类似C:\PathToYourStuff\src\myApp\ajax2.php,这不是有效的URL格式。这就是为什么错误消息指出

Could not resolve host: C

解释上面的URL将意味着C是主机名的路径,因为冒号(“:”)是分开的端口的主机名中的一部分URL。 (从URL解析器的角度来看,这背后的部分是无稽之谈,但它甚至没有达到那么远,因为假定的主机名无法解析。)

所以你必须改用的是一个URL指向那个文件,例如像http://localhost/path-to-your-stuff/src/myApp/ajax2.php

所以更改您的代码这样的事情,并根据需要调整网址:

$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