php中curl用法就是:创建curl会话 -> 配置参数 -> 执行 -> 关闭会话。
- 发起wordpress项目请求
<?php
add_action('save_post', 'demo', 10 ,3); //save_post钩子可更换成你业务需要的
function demo()
{
$data = array(
"name" => "open",
"msg" => "success"
);
$curl = curl_init( 'dev.test1.com/api/show' ); //dev.test1.com是你的项目路径
curl_setopt( $curl, CURLOPT_POST, true);//post
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data); //$data带参数
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $curl );//响应回来的数据
$res = json_decode($response);
curl_close( $curl );
}
?>
laravel项目在routes下api.php
1.首先定义api路由:在routes目录下api.php注册api路由
Route::post('/show', 'CategoriesController@show');//wp是post提交lar这边也要post
2.CategoriesController控制器创建方法(获取数据并响应)
public function show(Request $request)
{
$res = $request->input(); //获取到的数据
if(!empty($res)){
info($res); //wordpress传过来的 $data 保存在日记中查看
return response()->json(['result'=>'success']);//响应成功
}else{
return response()->json(['result'=>'error']);
}
}
有不足之处请指教出来谢谢。