1

I have a small PHP script that is supposed to get all the values from a web service´s JSON. But after decoding it, I keep getting error 4.

$url_de = "http://ws-old.parlament.ch/factions?format=json&lang=de&pretty=true";    
$jsonObject = json_decode(file_get_contents($url_de));

The string $url_de has the URL of website´s JSON file that contains the following when viewed in a browser:

[
 {
"name": "Liberale Fraktion",
"shortName": "Fraktion L",
"code": "FRA_7_",
"abbreviation": "L",
"id": 7,
"updated": "2015-08-31T08:53:40Z"
 },
  {
    "name": "Sozialdemokratische Fraktion",
    "shortName": "Fraktion S",
    "code": "FRA_2_",
    "abbreviation": "S",
    "id": 2,
    "updated": "2015-08-24T15:28:11Z",
    "hasMorePages": false
  }
]

When using var_dump I keep getting NULL. Any ideas how to fix it? I would like to output certain keys and their values.

EDIT: This is the HTML output when using var_dump enter image description here Thanks for the help in advance.

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Apr 1, 2020 at 7:59

2 Answers 2

2

I've emulated your Webservice call that working on the web browser with PHP curl and it works fine for me, Hopes it will work for you also.

Note You'll not require some of the Headers so you can discard them. The minimum you need like this example https://3v4l.org/QaUTl

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ws-old.parlament.ch/factions?format=json&lang=de&pretty=true');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'Pragma: no-cache';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: en,es;q=0.9,en-US;q=0.8,en-IN;q=0.7';
$headers[] = 'Cookie: SessionId=ybsj42455jmgxy55ujqn4c45; BIGipServerpool_frontend_ext_prod=210882826.20480.0000; PD_WebServices_Culture=de; TS011b62d7=0105a873d69ab2bf15b10d359e436b495a612d9693e8d00bd58e025621fb968b99223e473d2fe8154f4ff4acea8109792c30c6a2eb429a3297a4304e770889c54c3f621f7e330185c6d1c4c58523f82f5f168cf4a228fac9d6fb500eb6007c20da3a2f59aa21125d29b5c2c0e2eb2436b88eb5df00';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print '<pre>';
print_r(json_decode($result));
print '</pre>';
?>
Sign up to request clarification or add additional context in comments.

4 Comments

this much is enough: 3v4l.org/QaUTl (checked via phpfiddle and i think good to go)
@AnantSingh---AlivetoDie thanks you sir, I'm going to edit it
don't edit it. Just add my example link in your answer. because your answer is going to be helpful for others in different different scenario.
Okay I'll do, Thanks!
0

This works too:

$url = 'http://ws-old.parlament.ch/factions/1?format=json&lang=de';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36");

$data = curl_exec($ch);
curl_close($ch);
$jsonObject = json_decode($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.