如何从API获取数据-PHP-curl [英] How to get data from API - php - curl
问题描述
如何从api网址获取数据(图片)?
How can I get data ( image ) from api url?
Api结果:
info = { "title" : "aaaaa", "image" : "bbbbb" };
Api网址:
http://www.example.com/api
代码:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/api');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $result->image;
?>
Array
(
[curlResult] => info = { "title" : "aaaaa", "image" : "bbbbb" };
[info] => Array
(
[url] => http://127.0.0.1/api.txt
[content_type] => text/plain
[http_code] => 200
[header_size] => 237
[request_size] => 55
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 48
[speed_download] => 48
[speed_upload] => 0
[download_content_length] => 48
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] => 127.0.0.1
[primary_port] => 80
[local_ip] => 127.0.0.1
[local_port] => 61662
[redirect_url] =>
)
[errno] => 0
[error] =>
)
推荐答案
尝试调试的一种好方法是检查curl信息数组或curl错误.
A good way to try and debug this would be to check the curl info array, or curl error.
<?php
/**
* Handles making a cURL request
*
* @param string $url URL to call out to for information.
* @param bool $callDetails Optional condition to allow for extended
* information return including error and getinfo details.
*
* @return array $returnGroup cURL response and optional details.
*/
function makeRequest($url, $callDetails = false)
{
// Set handle
$ch = curl_init($url);
// Set options
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute curl handle add results to data return array.
$result = curl_exec($ch);
$returnGroup = ['curlResult' => $result,];
// If details of curl execution are asked for add them to return group.
if ($callDetails) {
$returnGroup['info'] = curl_getinfo($ch);
$returnGroup['errno'] = curl_errno($ch);
$returnGroup['error'] = curl_error($ch);
}
// Close cURL and return response.
curl_close($ch);
return $returnGroup;
}
$url = /* some url */
$response = makeRequest($url, true);
// Check your return to make sure you are making a successful connection.
echo '<pre>';
print_r($response);
?>
看看您是否成功建立了连接,然后确切地从响应中获得了什么,应该使您更好地了解如何处理它.
Seeing if you are successfully making the connection, and then also exactly what you are getting back in the response should give you a better idea of how to handle parsing it.
编辑
看起来有些非JSON字符串数据正在阻止json_decode
函数正常工作.这不理想,但是确实发生了.在这种特定情况下,以下方法应该会有所帮助,但这只是解决涉及不良数据的问题的解决方法.
It looks like there is some non-JSON string data that is preventing the json_decode
function from working. This is less than ideal, but it happens. Following method should help in this specific case, but is just a workaround to an issue involving bad data.
// continuing from above :) formatted for readability
/**
* Separates JSON string from unnecessary data, parses into
* an object and returns.
*
* @param string $curlResult String returned from cURL call.
*
* @return object $jsonObject Parsed JSON object.
*/
function cleanJsonString($curlResult) {
// Separates result string into array at " = ".
$responseChunks = explode(" = ", $curlResult);
// Removes semicolon from end of JSON string.
$jsonString = substr($responseChunks[1], -1);
// Parse JSON string into object.
$jsonObject = json_decode($jsonString);
return $jsonObject;
}
// Clean result.
$jsonObject = cleanJsonString($response['curlResult']);
// Print image object property.
echo $jsonObject->image;
这篇关于如何从API获取数据-PHP-curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!