如何将IBM Watson的cURL命令转换为WordPress可用的PHP代码
Converting Watson Visual Recognition cURL to PHP (WordPress Compatible)
Let's break down how to translate your cURL command into working PHP code, optimized for WordPress. We'll cover both standard PHP cURL and WordPress's recommended HTTP API approach.
Key Components of the Original cURL Command
Your cURL sends a POST request with two form fields:
images_file: The image file (fruitbowl.jpg)parameters: A JSON object specifying classifier IDs, owners, and threshold
1. Standard PHP cURL Implementation
This is a direct translation using PHP's built-in cURL functions:
<?php // Replace these with your actual values $api_key = 'your_api_key_here'; $image_path = '/full/path/to/fruitbowl.jpg'; // Define your parameters as an array $parameters = array( 'classifier_ids' => array('My_Model_ID', 'default'), 'owners' => array('me'), 'threshold' => 0.6 ); $json_parameters = json_encode($parameters); // Prepare the image file using CURLFile (avoids deprecated @ syntax) $image_file = new CURLFile(realpath($image_path), 'image/jpeg', 'fruitbowl.jpg'); // Set up cURL options $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={$api_key}&version=2016-05-20"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'images_file' => $image_file, 'parameters' => $json_parameters )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Keep this true for production // Execute the request $response = curl_exec($ch); // Handle errors if (curl_errno($ch)) { $error = curl_error($ch); echo "cURL Error: " . $error; } else { // Decode the JSON response $result = json_decode($response, true); // Process the result as needed print_r($result); } // Close the cURL handle curl_close($ch); ?>
2. WordPress HTTP API Implementation (Recommended)
WordPress has its own HTTP API (wp_remote_post) that handles SSL, proxies, and other edge cases better than raw cURL. Here's how to use it:
<?php // Replace these with your actual values $api_key = 'your_api_key_here'; $image_path = '/full/path/to/fruitbowl.jpg'; // Define your parameters $parameters = array( 'classifier_ids' => array('My_Model_ID', 'default'), 'owners' => array('me'), 'threshold' => 0.6 ); // Prepare the request body $body = array( 'images_file' => new CURLFile(realpath($image_path), 'image/jpeg', 'fruitbowl.jpg'), 'parameters' => json_encode($parameters) ); // Set up request arguments $args = array( 'method' => 'POST', 'body' => $body, 'timeout' => 30, 'sslverify' => true // Disable only for local testing, not production ); // Send the request $url = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={$api_key}&version=2016-05-20"; $response = wp_remote_post($url, $args); // Handle response/errors if (is_wp_error($response)) { echo "Request Error: " . $response->get_error_message(); } else { $response_body = wp_remote_retrieve_body($response); $result = json_decode($response_body, true); // Process the result print_r($result); } ?>
Important Notes
- File Path: Ensure
$image_pathis the full server path to your image (useABSPATHin WordPress to get the root directory if needed). - API Key: Replace
your_api_key_herewith your actual Watson Visual Recognition API key. - SSL Verification: Keep
sslverifyset totruein production to avoid security risks. - Error Handling: Add proper error handling based on your site's needs (e.g., logging instead of echoing errors).
内容的提问来源于stack exchange,提问作者Leonardo Feriato
相关产品推荐
相关产品推荐

