我想在PHP执行Curl代码。
curl -X POST 'https://api.sightengine.com/1.0/check.json' \ -F 'api_user=1********5' \ -F 'api_secret=q**************Q' \ -F 'media=@/full/path/to/image.jpg' \ -F 'models=nudity'上面的代码有四个参数传递给api。 我在PHP代码下面尝试执行:
function image() { $body_data = http_build_query(array('api_user' => 1********5, 'api_secret' => 'q**************Q', 'media' => $_FILES['image']['name'], 'models' => 'nudity')); // Configure cURL $image_curl = curl_init(); curl_setopt($image_curl, CURLOPT_URL, "https://api.sightengine.com/1.0/check.json"); curl_setopt($image_curl, CURLOPT_POST, true); // Use POST curl_setopt($image_curl, CURLOPT_POSTFIELDS, $body_data); // Setup post body curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true); // Receive server response // Execute request and read responce $session_response = curl_exec($image_curl); $response = json_decode($session_response); print_r($response); }响应:
stdClass Object([status] => failure [request] => stdClass Object([id] => req_2365jHPuLcC6Bydh7WNd7 [timestamp] => 1512542730.57 [operations] => 0)[error] => stdClass Object([type] => argument_error [code] => 4 [message] =>没有指定媒体))
现在问题来自media和models参数。
我不确定media参数的文件路径有一些问题。 我想要和前面的额外@路径。
我定义了$body_data数组中的所有参数并正确地将它们传递给CURLOPT_URL 。
请帮我解决这个问题。 当我在POSTMAN中POSTMAN它时,它工作正常。 提前致谢。
I want to execute Curl code inside PHP.
curl -X POST 'https://api.sightengine.com/1.0/check.json' \ -F 'api_user=1********5' \ -F 'api_secret=q**************Q' \ -F 'media=@/full/path/to/image.jpg' \ -F 'models=nudity'Above code has four parameters to pass to the api. Below PHP code I tried to execute:
function image() { $body_data = http_build_query(array('api_user' => 1********5, 'api_secret' => 'q**************Q', 'media' => $_FILES['image']['name'], 'models' => 'nudity')); // Configure cURL $image_curl = curl_init(); curl_setopt($image_curl, CURLOPT_URL, "https://api.sightengine.com/1.0/check.json"); curl_setopt($image_curl, CURLOPT_POST, true); // Use POST curl_setopt($image_curl, CURLOPT_POSTFIELDS, $body_data); // Setup post body curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true); // Receive server response // Execute request and read responce $session_response = curl_exec($image_curl); $response = json_decode($session_response); print_r($response); }Response:
stdClass Object ( [status] => failure [request] => stdClass Object ( [id] => req_2365jHPuLcC6Bydh7WNd7 [timestamp] => 1512542730.57 [operations] => 0 ) [error] => stdClass Object ( [type] => argument_error [code] => 4 [message] => No media specified ) )
Now the problem is fro the media and models parameters.
I'm not sure the file path to media parameter has some problem. And do I want to and an additional @ in-front of the path.
I'm I defined all parameters in $body_data array and passing them to the CURLOPT_URL properly.
Please help me to solve this issue. When I try this in POSTMAN it works fine. Thanks in advance.
最满意答案
你不需要使用http_build_query 。
PHP 5.5,5.6等支持通过@符号,但在PHP 7中不推荐使用。现在我们可以使用https://secure.php.net/manual/en/class.curlfile.php 。
$body_data = array( 'api_user' => '3454', 'api_secret' => 'q**************Q', 'models' => 'nudity' ); $body_data['media'] = new CurlFile(realpath('file.jpg')); // Configure cURL $image_curl = curl_init(); curl_setopt($image_curl, CURLOPT_URL, "https://api.sightengine.com/1.0/check.json"); curl_setopt($image_curl, CURLOPT_POST, true); // Use POST curl_setopt($image_curl, CURLOPT_POSTFIELDS, $body_data); // Setup post body curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true); // Receive server response // Execute request and read responce $session_response = curl_exec($image_curl); $response = json_decode($session_response);You don't need to use http_build_query.
PHP 5.5, 5.6 etc supported to pass @ sign, but deprecated in PHP 7. Now we can use https://secure.php.net/manual/en/class.curlfile.php .
$body_data = array( 'api_user' => '3454', 'api_secret' => 'q**************Q', 'models' => 'nudity' ); $body_data['media'] = new CurlFile(realpath('file.jpg')); // Configure cURL $image_curl = curl_init(); curl_setopt($image_curl, CURLOPT_URL, "https://api.sightengine.com/1.0/check.json"); curl_setopt($image_curl, CURLOPT_POST, true); // Use POST curl_setopt($image_curl, CURLOPT_POSTFIELDS, $body_data); // Setup post body curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true); // Receive server response // Execute request and read responce $session_response = curl_exec($image_curl); $response = json_decode($session_response);更多推荐
发布评论