I want to get data from api..
but I have trouble with json ..I can not get json string .. it print as normal string or xml..
I try multi method from net but no solution..
this my code:
$url = "http://fv4online.com/egov_api/v1/students/7";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept:
application/json;charset=UTF-8'));
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
print_r($output);
$t=json_decode($output,true);
print_r($t);
// close curl resource to free up system resources
curl_close($ch);
result of print ($output) is string:
7345345343testtesttesttest2000testliterary4564620171testDamascusar_lang300600350en_lang160400200fr_lang160400200national80200100history120300200geograph120300200philosophy160400300religion802001501700155060successful
and result of print(var_dump($output)) is xml:
'
7345345343testtesttesttest2000testliterary4564620171testDamascusar_lang<'... (length=1544)
note: Previously I was able to get this data without any problems, but suddenly the problem arose
That string contains all values the XML from the url returns, just without the tags around them. If you do 'View Source' you'll see more.
You get XML, not JSON. Could be they've changed that (which would be a bit odd, but I've seen worse). If you want is as JSON:
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
Related
I'm setting up a little webservice usable via an app on Android.
The function in the server is this:
$lat=43.0552592;
$lng=12.4483577;
$radius=2000;
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey;
$urlW = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=43.0552592,12.4483577&radius=2000&types=restaurant&sensor=false&key=XXXXxxxXXxxXXxxxxXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
return $response;
The trouble is that if I query Google with the $url (as in the above example) it returns the JSON with 'INVALID REQUEST', but if the query at the first curl_opt is done with $curlW will works like a charm.
While debugging that I've discovered, making $url return, that gets every & converted (before the curl_init!) in &...!
So I've tried almost every PHP string function to force every & decode or replacing every &entities to & only without any result.
Any suggestion?
you've done something wrong,
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey
should be
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$radius."&types=restaurant&sensor=false&key=".$apiKey;
but i don't guess that is what is doing the problem, try using file_get_contents() instead of curl, so $response will be like this:
$response = file_get_contents($url);
Here is my code,
$url= 'http://dummyhost:8080/admin/EditSubscriber?jsonString={"sub_Id":3,"sub_Fname":"messi","sub_Lname":"lionel"}';
$data_string="";
$request = new HTTPRequest($url, HTTP_METH_POST);
$request->setRawPostData($data_string);
$request->send();
$response = $request->getResponseBody();
$response= json_decode($response, true);
at the end of url JSON string is concatenated according to server requirement
but in response there is nothing i get in response variable.
What is wrong with this as when i make this request using chrome extension it shows me the result updated. And when i use the $url= "http://dummyhost:8080/admin/ViewSubsriber?jsonString={"sub_Name":"messi","sub_Password":"password"}";
i get the desired result.
i've used curl as well'
i've used Curl as well like this
$ch = curl_init($url);
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))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
but the same result i get that is nothing
If you have created JSON string at your own keep following things in mind:
Space in string might result in unnatural behavior from server so for each of the varible or atleast for strings use
urlecode(yourvariable);
then chek the string online wether the JSON string is valid or not like this
http://json.parser.online.fr/
Like Brant says use
$json = file_get_contents('php:://input');
for raw data instead of using the empty $data_string="";
Your posted variable, $data_string, is empty. You are using POST and sending empty data, but then also sending a query string. It seems you are mixing GET and POST methods here. You need to actually post your JSON string in the posted data.
If you are posting raw JSON string using application/JSON content type, the post data will need to be read from raw input like this
$json = file_get_contents('php:://input');
This is because $_POST is only automatically populated by PHP for form-encoded content types.
I would also recommend sticking with curl for such usage.
I have an XML file I need to pass.
http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true
I used PHP's CURL to load it into the page:
$url = "http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$xml = curl_exec($ch);
curl_close($ch);
echo htmlentities($xml, ENT_QUOTES, "UTF-8");
The output is a JSON file, which I wasn't expecting. No matter, I continued working with the JSON but can't seem to parse it.
I had thought this would work:
$obj=json_decode($xml);
echo $obj[stopTime][0][phase];
With a result of 'departure'. I've spent a good while on this yet can't get it to output anything.
Anyone help? And anyone know at which point it switched from an XML file to JSON format?
the output is a xml-string.
try to use php's xml functionality delivered by SimpleXml:
SimpleXml
as soon as you successfully parsed the string you can iterate its nodes like an array
foreach ($xmlElement as $node) {
echo $node->attributeName;
}
Hi I'm a little new at CURL, but I'm trying to request some json data and then parse the results. I am having success with retrieving the data, but I can't handle the response. Here's the code
function bitBucketCurl($url)
{
global $bitPassword;
global $bitUsername;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$bitUsername:$bitPassword");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$commitinfo = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
return $commitinfo;
}
$json = bitBucketCurl($url);
echo $json; // This seems to work in that, when I load the page, I can see the json data
//turn json data into an array - this is what does not seem to be working
$obj_a = json_decode($json, true);
print_r ($obj_a); //the result is simply a 1 rather than the array I would expect
The basic problem is the json data shows up when I echo $json but when I try to turn that data into an array it doesn't work. When I print the array, I just get a '1'.
I got the required result by adding the following line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
I've run into trouble with the following php code:
<?php
$url = "http://api.ean.com/ean-services/rs/hotel/v3/list? minorRev=1&cid=55505&apiKey=58x5kuujub8xbb5tzv3a2a8q&locale=en_US¤cyCode=USD&xml= <HotelListRequest><destinationString>Seattle</destinationString> <arrivalDate>08/01/2011</arrivalDate><departureDate>08/03/2011</departureDate><RoomGroup> <Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup> <numberOfResults>1</numberOfResults></HotelListRequest>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$contents = curl_exec ($ch);
echo $contents;
curl_close($ch);
?>
The problem is that $contents contains markup that's not XML at all, so I can't parse it. It's confusing b/c entering the URL in my browser's address bar will display the XML document, but I can't seem to get a valid XML doc w/ this code.
Here is a snippet of the string that gets returned:
{"HotelListResponse":{"customerSessionId":"0ABAA83D-4428-4913-0382-28FBB1901EFC","numberOfRoomsRequested":1,"moreResultsAvailable":true,"cacheKey":"-32344284:1303828fbb1:-1ef9","cacheLocation":"10.186.168.61:7305","HotelList":{"#size":"1","HotelSummary":{"#order":"0"
Could someone explain to me where I'm going wrong?
Thx.
Instead of trying to get XML, which may not be provided, you could always work with what you have, which appears to be JSON.
$response = json_decode( $contents, true );
This will give you an associative array of your data, which can be much easier to work with.
Try to remove spaces: "/v3/list? minorRev=1" -> "/v3/list?minorRev=1"
Make your URL correct, like
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?type=xml&minorRev=1&cid=55505&apiKey=58x5kuujub8xbb5tzv3a2a8q&locale=en_US¤cyCode=USD&xml=%3CHotelListRequest%3E%3CdestinationString%3ESeattle%3C/destinationString%3E%3CarrivalDate%3E08/01/2011%3C/arrivalDate%3E%3CdepartureDate%3E08/03/2011%3C/departureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E2%3C/numberOfAdults%3E%3C/Room%3E%3C/RoomGroup%3E%20%3CnumberOfResults%3E1%3C/numberOfResults%3E%3C/HotelListRequest%3E';
Add option to accept xml only -- in browser we have such header -- in curl -- no:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
PROFIT!!!