2

Using CURL here we retrieve data and print then save to file after that insert it into database.

$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;
}

The Result json is fine. now i want to save it in json file?

2
  • you could remove CURLOPT_CUSTOMREQUEST => "GET", and CURLOPT_POSTFIELDS=>"", as they are superflous and contradict one another. Commented May 21, 2019 at 10:32
  • OK i will do that. thanks man!! Commented May 21, 2019 at 10:51

1 Answer 1

1

to save a string to a file in php, you can use file_put_contents()

file_put_contents doc

file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] ) : int

You can update your code as follows:

if ($err){
    echo "cURL Error #:" . $err;
} else {
    echo $response;

    //Write response to file
    file_put_contents("my_file.json", $response)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice !! It's Done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.