I'm using the below curl request which returns a XML structured response. The response however seems wrong (first line: string(744978) "<?xml etc). I normally json_decode it, however that does not seem to work. Is this an issue in the endpoint or am I doing something wrong? I would like to convert the response to an array so I can store it in a database.
REQUEST
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
RESPONSE
use this function for result like
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$result = api_result($result);
curl_close($ch);
function api_result($result){
$plainXML = $this->mungXML($result);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arrayResult;
}
<!-- begin snippet: js hide: false console: true babel: false -->
function xml_to_json_feed() {
$url = 'http://x';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$xml = simplexml_load_string($result);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
curl_close($ch);
return $array;
}
Related
I am getting this response
[{"id":15702671,"payment_id":15702666,"amount":20,"metadata":{},"source":{"id":"347862165","name":"Test Test","type":"collector"},"date_created":"2018-08-28T08:11:15.000-04:00","unique_sequence_number":null,"status":"approved"}]
This is my Code
$url = "https://api.mercadopago.com/v1/payments/".$paymeny_id."/refunds?access_token=TEST-4015220099523401-082300-232be80199d9b1a9b0bac72d6af99fd2-347862165";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);
curl_close($ch);
how to save response value in variable ?
try this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$var = curl_exec($ch)
Hope this works!
to get the id
$var = json_decode($var);
$id = $var[0]->id;
you need to add CURLOPT_RETURNTRANSFER option in your curl object to get the response using curl as
$url = "https://api.mercadopago.com/v1/payments/".$paymeny_id."/refunds?access_token=TEST-4015220099523401-082300-232be80199d9b1a9b0bac72d6af99fd2-347862165";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
I am using a cURL request to get XML data, which works fine in the brower but returns text in the PHP cURL. I have looked at several similar questions here and tried the answers, with no luck. Here's the code.
$url = 'http://forecast.weather.gov/MapClick.phplat=38.4247341&lon=-86.9624086&FcstType=xml';
$agent = 'Myapp/v1.0 (http://example.org;webmaster#example.org)';
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_URL, $url);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($rCURL, CURLOPT_USERAGENT, $agent);
curl_setopt($rCURL, CURLOPT_HTTPHEADER, 'Content-Type: application/xml');
curl_setopt($rCURL, CURLOPT_BINARYTRANSFER, 1);
$aData = curl_exec($rCURL);
$error = curl_error($CURL);
curl_close($rCURL);
if ($error)
echo ($error);
else echo ($aData);
Your request already contains FcstType parameter to specify that response should be returned in XML format: FcstType=xml
Once XML response (string) is returned it could be parsed with SimpleXMLElement class as demonstrated below:
$url = 'http://forecast.weather.gov/MapClick.php?lat=38.4247341&lon=-86.9624086&FcstType=xml';
$agent = 'Myapp/v1.0 (http://example.org;webmaster#example.org)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($response);
foreach($xml->period as $period){
echo $period->text . "\n";
}
here is the link what Im trying with:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=47.02842388681676|18.51792812347412&destinations=47.4735911253571|20.596618652343803&key=*******&language=en&units=metric
this works great from browsers. But gives FALSE from php:
$ch = curl_init();
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='.urlencode($lat1).'|'.urlencode($long1).'&destinations='.urlencode($lat2).'|'.urlencode($long2).'&key='.urlencode('**********').'&language=en&units=metric';
echo $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
curl_close($ch);
//$c = json_decode($c);
echo '<pre>'; var_dump ($c);die;
You can try with file_get_contents:
$content = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=47.02842388681676|18.51792812347412&destinations=47.4735911253571|20.596618652343803&key=*******&language=en&units=metric');
$content = json_decode($content);
print_r($content);
Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);
I am getting bool(false) when I var_dump $information I can access the API fine via the browser just not via the code below why?
$json = file_get_contents('API URL');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $json);
curl_setopt($ch, CURLOPT_USERPWD, "user:pw");
curl_error($ch);
$result = curl_exec($ch);
curl_close($ch);
$information = json_decode($result, true);
var_dump($result);
This line
$json = file_get_contents('API URL');
should be
$json = 'API_URL';