{{1}}
您好, 我正在使用REST api post方法,键入JSON以使用curl函数发布数据。但它根本不起作用,没有输出。
我通过使用Fiefox插件RESTClient对此进行了测试。 它的工作正常,能够将数据发布到我的数据库中。 使用PHP curl方法发布数据时,我陷入困境。以上是我从互联网上采取的一个例子,但仍然无法正常工作。需要老年人的建议。
答案 0 :(得分:1)
// Handle POST requests to /articles
$app->post('/articles', function () use ($app) {
try {
// get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
$count = count($input);
for ($x = 0; $x < $count; $x++) {
// store article record
$article = R::dispense('articles');
$article->title = (string)$input[$x]->title;
$article->url = (string)$input[$x]->url;
$article->date = (string)$input[$x]->date;
$id = R::store($article);
// return JSON-encoded response body
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($article));
}
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
答案 1 :(得分:0)
我知道,从上面的示例中,url是标准的http请求,但代码引用了ssl。此外,您设置RETURNTRANSFER
但不回显/打印结果 - 仅打印错误(如果有)。我发现拥有cacert.pem
- download from here的副本至关重要
〜根据您的服务器配置编辑路径。如果该网站不是https,那么无论如何它都会被忽略。
function processCurlJsonrequest($URL, $fieldString) {
$cacert=realpath( 'c:/wwwroot/cacert.pem' );
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if( parse_url( $URL,PHP_URL_SCHEME )=='https' ){
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_CAINFO, $cacert );
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fieldString ) );
curl_setopt($ch, CURLOPT_POST, true);
$results=curl_exec($ch);
$errors=curl_error($ch);
curl_close($ch);
return array(
'results'=>$results,
'errors'=>$errors
);
}
注意到函数返回一个数组,在处理响应时你需要考虑到这个....
$data_string = array("title" => "jdsdfds","url"=>"sdfdfd","date"=>"2014-01-30");
$response = processCurlJsonrequest('http://localhost/articles',$data_string);
echo '<pre>',print_r($response,true),'</pre>';
答案 2 :(得分:0)
client.php:
<?php
$Username = "TestUsername";
$Password = "Password123";
$Email = "mail@email.tld";
$Url = "http://localhost/your/path/server.php?Username=$Username&Password=$Password&Email=$Email";
//send request:
$Client = curl_init($Url);
curl_setopt($Client, CURLOPT_RETURNTRANSFER, 1);
//response:
$Response = curl_exec($Client);
//decode:
$Result = json_decode($Response);
if($Result->Status==200){
$Output .= "Success, $Result->StatusMessage ($Result->Status)<br/> Data:<br/>";
$Output .= "Username: " . $Result->Information->Username . "<br/>";
$Output .= "Password: " . $Result->Information->Password . "<br/>";
$Output .= "Email: " . $Result->Information->Email . "<br/>";
echo $Output;
}
else{
echo "an error occured.";
}
Server.php:
<?php
header("Content-Type:application/json");
$Username = $_GET['Username'];
$Password = $_GET['Password'];
$Email = $_GET['Email'];
$HTTPSTATUS = 200;
$HTTP_MESSAGE = "RECORD ADDED.";
$Info = array(
'Username' => $Username,
'Password' => $Password,
'Email' => $Email
);
header("HTTP/1.1 $HTTPSTATUS $HTTP_MESSAGE");
$Response['Status'] = $HTTPSTATUS;
$Response['StatusMessage'] = $HTTP_MESSAGE;
$Response['Information'] = $Info;
$Return = json_encode($Response);
echo $Return;
这将是结果:
Success, RECORD ADDED. (200)data:
Username: TestUsername
Password: Password123
Email: mail@email.tld
现在,我不是这方面的专家,但这对我来说就像是一种魅力。
答案 3 :(得分:0)
我确实改变了一些代码:
// store article record
$article = R::dispense('articles');
$article->title = (string)$input[$x]->title;
$article->url = (string)$input[$x]->url;
$article->date = (string)$input[$x]->date;
$id = R::store($article);
到此:
// store article record
$article = R::dispense('articles');
$article->title = $input[$x]['title'];
$article->url = $input[$x]['url'];
$article->date = $input[$x]['date'];
$id = R::store($article);
现在我可以从Firefox rest-client插件发帖了。能够在db中发布和创建数据。但是,当我尝试使用PHP卷曲代码时,它仍无效。
答案 4 :(得分:0)
你应该试试这个
$url = 'Your url';
$args = 'Your argumnts';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$result = curl_exec($ch);
curl_close($ch);
return $result ? json_decode($result, true) : false;