Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 Laravel api发送了带有curl的json post_Php_Laravel - Fatal编程技术网

PHP Laravel api发送了带有curl的json post

PHP Laravel api发送了带有curl的json post,php,laravel,Php,Laravel,我的问题如下 我有一个计数器api,它使用laravel。我需要将post作为json发送到此api,但是当我发送时没有标题,419页面返回为过期,当我发送标题时,我得到一个csrf令牌missmatch错误。但是,我从meta中获取csrf令牌并将其放入头中 我想指出我没有使用Laravel,我将发布的api使用的是Laravel。 我的代码: <?php $data = 'JSON DATA'; $wow = json_encode($data); $ch = curl_init

我的问题如下

我有一个计数器api,它使用laravel。我需要将post作为json发送到此api,但是当我发送时没有标题,
419
页面返回为过期,当我发送标题时,我得到一个
csrf令牌missmatch
错误。但是,我从meta中获取csrf令牌并将其放入头中

我想指出我没有使用Laravel,我将发布的api使用的是Laravel。

我的代码:

<?php 
$data = 'JSON DATA'; 
$wow = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"laravel api url");

$dom = new DOMDocument;
$dom->loadHTML($resultado);
$tags = $dom->getElementsByTagName('meta');
for ($i = 0; $i < $tags->length; $i++) {
    $grab = $tags->item($i);
    if ($grab->getAttribute('name') == 'csrf-token') {
        $token = $grab->getAttribute('content');
    }
}

ob_start();      // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean();  // stop preventing output

curl_close ($ch);
unset($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"LARAVEL API URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $wow);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json, text/plain, */*',
    'Accept-Encoding: gzip, deflate, br',
    'X-CSRF-TOKEN: '.$token.''));

$buf2 = curl_exec ($ch);

curl_close ($ch);

echo htmlentities($buf2);
?>  

您可以完全禁用特定
路线的
CSRF令牌
,如果这对您的应用程序有意义的话

检查并编辑
app/Http/Middleware/VerifyCsrfToken.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'stripe/*',
        'http://example.com/foo/bar',
        'http://example.com/foo/*',
    ];
}

为什么不排除需要CSRF的特定路由?@Ron,因为这次我得到了419页过期错误。419正是由于CSRF。@Ron我猜是这样的,因为当我通过标题删除CSRF行时,我得到了419。你可以在Laravel中声明,甚至没有检查特定路由的CSRF。。一旦你这么做了,就不会有419错误。。。因为没有过期的令牌。。