在php中运行curl(Run curl inside php)

编程入门 行业动态 更新时间:2024-10-24 09:16:33
php中运行curl(Run curl inside php)

我想在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);

更多推荐

本文发布于:2023-07-26 15:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:php   curl   Run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!