Reputation: 23
I have an API link, suppose it's https://abcd.com/get?name=abcd&place=efgh... When I use the link in Postman or even directly in browser address bar, it returns me JSON response just fine. But the problem is, when I use it in PHP Curl (running on localhost), it doesn't return anything.
The Curl format is like this:
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $myUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
]);
// Execute cURL request
$response = curl_exec($ch);
But all I see is a blank page or null result when I tried to print it. I tried to print out the error, and it gave me error code 301. My guess is it has something to do with CORS.
Can someone help me solve it as I'm kind of new to this?
I tried to look for similar problems that others faced and tried to implement possible solutions, but no luck.
Upvotes: 0
Views: 16