从PHP脚本中进行jsonp调用

时间:2021-01-04 01:23:38

I am not sure if this is even possible since I m new to PHP. I have a URL (for instance: http://www.mysite.com/data/response/) which gives me some cookie data as json respose ( {"cookie_name":"value","ttl":60} ). I have a php script that thows some output based on a cookie value. So, right on top of this php script I need to make a call to the above url, get the json response/interpret it and set a cookie based on the response. Can someone please help me with this?

我不确定这是否可能,因为我刚接触PHP。我有一个URL(例如:http://www.mysite.com/data/response/),它给了我一些cookie数据作为json respose({“cookie_name”:“value”,“ttl”:60})。我有一个PHP脚本,它根据cookie值提供一些输出。所以,在这个php脚本之上,我需要调用上面的url,获取json响应/解释它并根据响应设置一个cookie。有人可以帮我这个吗?

Many thanks in advance. L

提前谢谢了。大号

1 个解决方案

#1


1  

Here it is:

这里是:

$url = 'http://www.mysite.com/data/response/';
$result = file_get_contents($url);
$json = json_decode($result, true);

set_cookie('cookie_name', $json['value'], time() + ((int) $json['ttl']));

As cwallenpoole mentioned in comments below, make sure that allow_url_fopen runtime configuration variable is set to TRUE (it is by default). If not - use ini_set('allow_url_fopen', 1), but it may be restricted by safe mode I think :)

正如下面的评论中提到的cwallenpoole,请确保allow_url_fopen运行时配置变量设置为TRUE(默认情况下)。如果不是 - 使用ini_set('allow_url_fopen',1),但它可能受到安全模式的限制我认为:)

json_decode function has been introduced in PHP 5.2.0. If you have PHP >= 5.2 and json_decode is not available, please check if JSON extension is enabled (extension=json.so in your php.ini) and if you PHP was combiled with --disable-json flag.

jince_decode函数已在PHP 5.2.0中引入。如果你有PHP> = 5.2并且json_decode不可用,请检查是否启用了JSON扩展(扩展名= php.ini中的json.so)以及PHP是否与--disable-json标志组合。

If you use pre-5.2 PHP then you can use following code (introduced by Anonymous user on PHP.net):

如果您使用5.2之前的PHP,那么您可以使用以下代码(由PHP.net上的匿名用户引入):

if ( !function_exists('json_decode') ){
function json_decode($json)
{
$comment = false;
$out = '$x=';

for ($i=0; $i<strlen($json); $i++)
{
    if (!$comment)
    {
        if (($json[$i] == '{') || ($json[$i] == '['))       $out .= ' array(';
        else if (($json[$i] == '}') || ($json[$i] == ']'))   $out .= ')';
        else if ($json[$i] == ':')    $out .= '=>';
        else                         $out .= $json[$i];          
    }
    else $out .= $json[$i];
    if ($json[$i] == '"' && $json[($i-1)]!="\\")    $comment = !$comment;
}
eval($out . ';');
return $x;
}
}

If allow_url_fopen is set to 0 and you cannot change it by ini_set or by setting it in your php.ini file, that you can stick to cURL (if cURL extension is enabled :)):

如果allow_url_fopen设置为0并且您无法通过ini_set或通过在php.ini文件中设置它来更改它,那么您可以坚持使用cURL(如果启用了cURL扩展:):

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER_HEADER, true);

$result = curl_exec($ch);
curl_close($ch);

The rest code should work as in first example. Hope it helps!

其余代码应该像第一个示例一样工作。希望能帮助到你!

#1


1  

Here it is:

这里是:

$url = 'http://www.mysite.com/data/response/';
$result = file_get_contents($url);
$json = json_decode($result, true);

set_cookie('cookie_name', $json['value'], time() + ((int) $json['ttl']));

As cwallenpoole mentioned in comments below, make sure that allow_url_fopen runtime configuration variable is set to TRUE (it is by default). If not - use ini_set('allow_url_fopen', 1), but it may be restricted by safe mode I think :)

正如下面的评论中提到的cwallenpoole,请确保allow_url_fopen运行时配置变量设置为TRUE(默认情况下)。如果不是 - 使用ini_set('allow_url_fopen',1),但它可能受到安全模式的限制我认为:)

json_decode function has been introduced in PHP 5.2.0. If you have PHP >= 5.2 and json_decode is not available, please check if JSON extension is enabled (extension=json.so in your php.ini) and if you PHP was combiled with --disable-json flag.

jince_decode函数已在PHP 5.2.0中引入。如果你有PHP> = 5.2并且json_decode不可用,请检查是否启用了JSON扩展(扩展名= php.ini中的json.so)以及PHP是否与--disable-json标志组合。

If you use pre-5.2 PHP then you can use following code (introduced by Anonymous user on PHP.net):

如果您使用5.2之前的PHP,那么您可以使用以下代码(由PHP.net上的匿名用户引入):

if ( !function_exists('json_decode') ){
function json_decode($json)
{
$comment = false;
$out = '$x=';

for ($i=0; $i<strlen($json); $i++)
{
    if (!$comment)
    {
        if (($json[$i] == '{') || ($json[$i] == '['))       $out .= ' array(';
        else if (($json[$i] == '}') || ($json[$i] == ']'))   $out .= ')';
        else if ($json[$i] == ':')    $out .= '=>';
        else                         $out .= $json[$i];          
    }
    else $out .= $json[$i];
    if ($json[$i] == '"' && $json[($i-1)]!="\\")    $comment = !$comment;
}
eval($out . ';');
return $x;
}
}

If allow_url_fopen is set to 0 and you cannot change it by ini_set or by setting it in your php.ini file, that you can stick to cURL (if cURL extension is enabled :)):

如果allow_url_fopen设置为0并且您无法通过ini_set或通过在php.ini文件中设置它来更改它,那么您可以坚持使用cURL(如果启用了cURL扩展:):

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER_HEADER, true);

$result = curl_exec($ch);
curl_close($ch);

The rest code should work as in first example. Hope it helps!

其余代码应该像第一个示例一样工作。希望能帮助到你!