Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过php调用api并从中获取json文件_Php_Json_.htaccess_Rest_Api - Fatal编程技术网

如何通过php调用api并从中获取json文件

如何通过php调用api并从中获取json文件,php,json,.htaccess,rest,api,Php,Json,.htaccess,Rest,Api,我想检查所有请求的url,如果url中包含“视频”文件夹,请将其重定向到API文件。然后API给了我一个json文件,其中只包含“respond:true”或“respond:false”。如果json文件中有respond:true,则必须显示url,如果json文件包含respond:false,则必须向用户显示预定义的简单403页面 我知道第一部分可以通过.htaccess文件中的简单代码实现,如下所示: RewriteRule ^your/special/folder/ /specifi

我想检查所有请求的url,如果url中包含“视频”文件夹,请将其重定向到API文件。然后API给了我一个json文件,其中只包含“respond:true”或“respond:false”。如果json文件中有respond:true,则必须显示url,如果json文件包含respond:false,则必须向用户显示预定义的简单403页面

我知道第一部分可以通过.htaccess文件中的简单代码实现,如下所示:

RewriteRule ^your/special/folder/ /specified/url [R,L]
但我不知道怎么做第二部分。我的意思是如何获得API的结果,它是json文件的形式,并检查它。

您可以使用CURL

获取请求

$url = 'http://example.com/api/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_json = curl_exec($ch);
curl_close($ch);
$response=json_decode($response_json, true);
    $postdata = array(
        'name' => 'Arfan'
    );

    $url = "https://example.com/api/user/create";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
发布请求

$url = 'http://example.com/api/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_json = curl_exec($ch);
curl_close($ch);
$response=json_decode($response_json, true);
    $postdata = array(
        'name' => 'Arfan'
    );

    $url = "https://example.com/api/user/create";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
您还可以使用文件获取内容获取API数据

$json = file_get_contents("$url")

您可以执行第二部分(调用API和响应):根据响应使用curl和process调用API:

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POST, false);
 curl_setopt($ch, CURLOPT_URL, "api_url_here");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $api_response_json = curl_exec($ch);
 curl_close($ch);
 //convert json to PHP array for further process
 $api_response_arr = json_decode($api_response_json);

 if($api_response_arr['respond'] == true ){
    //code for success here
 }else{
    // code for false here
 }

请注意:API的Json响应取决于API响应,如果API以Json格式给出响应(也可以基于参数)。

好的解决方案。但是我已经发现,如果我想保留基本url(用户请求的ulr),我不能使用.htaccess文件进行重定向!!那么,如何强制API使用php文件检查每个请求的url?@shekoufeh您可以在路由之前使用上述代码,或者在index.php中使用其他代码。放置条件一致我只想在需要视频文件时执行此操作。没有php文件。这部分我真的很困惑。。。