質問

I have a URL giving output a json object as: http://sachasauda.net/webservices/web_services_request/youtube_url?variable_name=test

its giving output on page is :

test={"name":"youtube_live_url","value":"https:\/\/www.youtube.com\/watch?v=xCjeLHoqdis"}

i want to use test.value, but i dont know, how it can be used in php code as variable or how can i assign test.value to a php variable.

I will appreciate any kind of help

役に立ちましたか?

解決

Leave off the variable_name=test parameter. Then it returns ordinary JSON, rather than a Javascript statement. Then you can call json_decode() on the result.

$json = file_get_contents("http://sachasauda.net/webservices/web_services_request/youtube_url");
$obj = json_decode($json, true);
$value = $obj['value'];
echo $value;

他のヒント

You can use file_get_contents() to fetch page content :

$page = file_get_contents('http://sachasauda.net/webservices/web_services_request/youtube_url');
$json_obj=trim($page);
// convert json to array 
$result_array=json_decode($json, true);
echo $result_array['value'];

And also can be done through CURL :

$url = 'http://sachasauda.net/webservices/web_services_request/youtube_url';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
$result=json_decode($data,true);
echo $result['value'];
curl_close($curl);
// remove ?variable_name=test from link
$page = file_get_contents('http://sachasauda.net/webservices/web_services_request/youtube_url');
$json = json_decode($page);

var_dump($json);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top