I am new in fetching data using APIs and need to fetch some particular columns data from a number of columns available. I am able to fetch all the data using API but wondering how to fetch particular columns. Below is my code:
<?php
$url = 'myurl';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"username\": \"test\", \"password\": \"test\"}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
$request = curl_init();
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($request, CURLOPT_HEADER, FALSE);
$result = curl_exec($request);
curl_close($request);
if ($result) {
$data = new SimpleXMLElement($result);
foreach ($data->Item as $entry) {
echo "XML Activity Name: " . $entry->ActivityName . "<br>";
}
} else {
exit();
}
?>
Related
Here is a query on the command line that works.
How can I get an answer to this request using PHP in the script?
curl -X GET https://megaservice.com/api/abonents -H 'X-AUTH-TOKEN: xxxxx-xxx-xxxxx' -H 'cache-control: no-cache'
This should work.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://megaservice.com/api/abonents",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"X-AUTH-TOKEN: xxxxx-xxx-xxxxx",
"cache-control: no-cache,no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
You can use cUrl:
$channel = curl_init('https://megaservice.com/api/abonents');
curl_setopt_array($channel, [
CURLOPT_RETURNTRANSFER => true, // Return curl-data to variable
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => [
'X-AUTH-TOKEN: xxxxx-xxx-xxxxx',
'cache-control: no-cache',
],
]);
$result = curl_exec($channel);
curl_close($channel); // Important thing - close channel for not load your server
var_dump($result); // Here's your result
You can post or get like this with curl,
function post($url, $data, $headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($headerArray){
curl_setopt($curl, CURLOPT_HTTPHEADER,$headerArray);
}
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function get($url,$userpwd = "",$headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"]){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($userpwd) {
curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
}
if($headerArray){
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
}
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
i have a problem when send request header to my restful, my restful checking request Authorize header, but when i send the header is missing.
My restful debug give me result NULL
i was tried to add CURLOPT_SSL_VERIFYPEER but also not working ,
anyone can help me out ?
here is my code :
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => "https://mysitehttps.domain",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"asdasd\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Authorization: 123HaHaHa",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 3b3fd06d-a8aa-65db-a917-c911fa0bb5d5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Thank you
I have tested this code and its working for me.
Your content-type is set to multi-part. Are you sure its not json. anyway try the code below
//initialize data variables that you want to send as post below
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('https://mysitehttps.domain');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"Authorization: 123HaHaHa",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 3b3fd06d-a8aa-65db-a917-c911fa0bb5d5"
)
);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://www.unocoin.com/trade?all');
$result = curl_exec($ch);
curl_close($ch);
$data = json_decode($result);
return $data;
JSON data is received when directly used in browser, but not with cURL in PHP.
Try like this way using php cURL,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.unocoin.com/trade?all=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET"
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Response:
{
"buy": 505189,
"sell": 487508,
"avg": 496348
}
$url = "http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA==&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617"
I try to request this by postman,add a header( Referer: http://static.youku.com/ ), it works.
But when I use CURL,it always show me nothing.I am not sure if cookies affect on this because I clear all the cookies on chrome,the postman still works.
I can't get useful cookies for this request by CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE,any body help me pls,thanks.
<?php
header("Content-type: text/html; charset=utf-8");
$headers = array(
'Referer' => 'http://static.youku.com/'
);
$url = 'http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA==&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($curl);
curl_close($curl);
?>
Thks for all,try to add params of postman into curl,this way works.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA%3D%3D&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 88976d57-03fd-7950-6c65-9d0f191010dc",
"referer: http://static.youku.com/";
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I am calling the API which is working fine with Postman but showing the error with the PHP CURL "Invalid Request".I have used the below code.
$postData = array(
'oauth_consumer_key' => 'XXXXXXXXXXXXXXXXXXXXXX',
'oauth_token' =>'',
'oauth_signature_method' => 'PLAINTEXT',
'oauth_timestamp' =>time(),
'oauth_nonce' =>'DQp8tIsd',
'oauth_version' =>'1.0',
'oauth_signature' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
);
$url = 'https://api.sandboxcernercare.com/oauth/access';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-
form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo "<pre>";
print_r($result);
I would say https, add :
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
You can generate the PHP CURL code from the Postman as well. it should be like below.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sandboxcernercare.com/oauth/access",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: OAuth oauth_consumer_key=\"XXXXXXXXXXXXXXXXX\",oauth_signature_method=\"PLAINTEXT\",oauth_timestamp=\"1493709577\",oauth_nonce=\"O9xz0w\",oauth_version=\"1.0\",oauth_signature=\"XXXXXXXXXXXXXXXXXXXXXXXXX\"",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}