是否使用PHP创建一个CURL post?
我有以下curl命令,它可以从windows命令行完美地运行:是否使用PHP创建一个CURL post?,curl,Curl,我有以下curl命令,它可以从windows命令行完美地运行: curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d "{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"somebuy@bla.com\"}" http://localhost:8080/users 因此,本文的jSon是: {\"name\":\"Na
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d "{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"somebuy@bla.com\"}" http://localhost:8080/users
因此,本文的jSon是:
{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"somebuy@bla.com\"}
我尝试使用以下PHP代码执行它:
$ch = curl_init(); // initialize curl handle
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 5s
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($request)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); // add POST fields
}
if($port==443) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch);
$error = curl_errno($ch);
curl_close($ch);
if ($error) {
$error_codes = $this->_get_curl_error_codes();
echo
'Sorry, but we are currently not able to connect to the database. Error code '.$error.': '.$error_codes[$error];
die();
}
我使用此代码如下:
$url = 'http://localhost/users';
$port = 8080;
$request = array();
$request['name'] = 'Name';
$request['password'] = '1234';
$request['email'] = 'somebuy@bla.com';
Json是这样创建的:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); // add POST fields
我的问题是,如果我从命令行运行它,它会工作,但是如果我使用上面的代码运行它,它就不工作了。知道为什么吗?控制台中的curl命令使用的是PUT,而您使用的是POST 尝试下一个选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
范例
如果不起作用,还可以添加标题
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json)));
还有一件事:
有一个
$this->\u get\u curl\u error\u codes()代码>但我在代码中并没有注意到这样的方法,控制台中的curl命令使用的是PUT,而您使用的是POST
尝试下一个选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
范例
如果不起作用,还可以添加标题
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json)));
还有一件事:
有一个$this->\u get\u curl\u error\u codes()代码>但我没有注意到您的代码中有这样的方法此代码可能会对您有所帮助
function get($url, $params=array()) {
$url = $url.'?'.http_build_query($params, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
此代码可能对您有所帮助
function get($url, $params=array()) {
$url = $url.'?'.http_build_query($params, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
在curl_exec取消注释此回显“curl error:”之后。旋度误差($ch);还有这意味着什么?$this->_get_curl_error_codes()?您的脚本不是类的一部分,因此在curl_exec取消对该echo“curl error:”的注释后,会引发错误。旋度误差($ch);还有这意味着什么?$this->_get_curl_error_codes()?您的脚本不是类的一部分,因此这将引发一个错误现在我得到了“请求实体太大”的结果是“现在允许使用方法”错误,但似乎我现在走的是正确的道路。我完全错过了“放置”部分…现在我得到了“请求实体太大”的结果是“方法现在允许”错误,但似乎我现在走的是正确的道路。我完全错过了“放”的部分。。。