Reputation: 1592
I want to use this curl command (works with Windows curl)
curl -k -i -H "Accept: application/json" https://brackets-registry.aboutweb.com/registryList
inside php curl. I tried this code:
<?php
$url="https://brackets-registry.aboutweb.com/registryList";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
But I don't get any json only html source code.
I think I am using the -H
and the -k
option, right? Don't know how to add -i
.
Upvotes: 0
Views: 94
Reputation: 44844
You need to set CURLOPT_RETURNTRANSFER
to 1 to get the returned data.
From the document
CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$url="https://brackets-registry.aboutweb.com/registryList";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
UPDATE : Its returning html content instead of JSON
So Content-type should be Accept
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Upvotes: 1
Reputation:
The name of the header you're sending is different in the two commands. The working curl
command line has Accept: application/json
, but your PHP code has Content-type: application/json
.
Upvotes: 1
Reputation: 1893
Why don't you use a library such as Guzzle to connect to the api?
In your command line, you use the Accept HTTP header, which negociate the content type of the response, but in your curl option you are using Content-Type, saying any information in your request body is in JSON, nothing about the response type you want. I therefore would have an extra line such as:
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
Upvotes: 1