在这里使用CURL,我们检索数据并打印,然后将其插入数据库后保存到文件。
$curl = curl_init();
$options=array(
CURLOPT_URL=>"http://api.abc.com/v1/products/search.json?q=ball",
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_ENCODING =>"",
CURLOPT_FOLLOWLOCATION =>true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT=>30,
CURLOPT_HTTP_VERSION=>CURL_HTTP_VERSION_1_0,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS=>"",
CURLOPT_HTTPHEADER=> array(
"authorization: AsiMemberAuth client_id=50041351&client_secret=55700485cc39f1",
"cache-control: no-cache"
),
CURLOPT_HEADER=> true
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err){
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Result json很好。现在我想将其保存在json文件中?
答案 0 :(得分:1)
要将字符串保存到php中的文件,可以使用file_put_contents()
file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] ) : int
您可以按以下方式更新代码:
if ($err){
echo "cURL Error #:" . $err;
} else {
echo $response;
//Write response to file
file_put_contents("my_file.json", $response)
}