PHP curl_exec停止执行脚本的其余部分,而没有错误日志

PHP curl_exec停止执行脚本的其余部分,而没有错误日志,php,php-curl,Php,Php Curl,我有一个使用curl和reporting设置运行的php脚本,但是它在使用curl\u exec()方法的行上停止执行,并且没有抛出错误: $fields = array( 'auth' => array( 'cId' => 'DEADBEEF-8675309-8675309-123123-4321', 'sig' => 'Not really a signature',

我有一个使用curl和reporting设置运行的php脚本,但是它在使用
curl\u exec()
方法的行上停止执行,并且没有抛出错误:

$fields = array(
        'auth' => array(
               'cId' => 'DEADBEEF-8675309-8675309-123123-4321',
               'sig' => 'Not really a signature',
               'data' => array(
                    'field' => 'pat',
                    'value' => '12',
                    'id1' => 'lasagna',
                    'id2' => 'peperoni'
                )

        ),
        'item1' => 'QPFMgH1TnCTLrylGeNs8yzYVVXxUgR0RHwj9jNwgXJJEfxODdoOKDOJLv66CSU5XKRfu4KYtDJB5rAmngxNrRDFpWU69oHMTlZoHAewuy3ft',
        'item2' => 'gMiGdw==',
        'tokenList' => array(
                "token", "list"
        )
);

$postfields = json_encode($fields);

error_reporting(E_ALL);
ini_set('display_errors', true);

$curl = curl_init('http://localhost:8080/endpoint');

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//$result = curl_exec($curl);

echo "before curl_exec" . "\n\n";
$response = json_decode(curl_exec($curl));
echo "after curl_exec" . "\n\n";

print_r($response);

编辑:忘记将$fields变量传递给curl_setopt()的带有json_encode的行包含在内

您的代码有几个问题:


您将“post”数据放入
$字段
,但是

curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
…您正在将
$postFields
传递给curl


假设这是一个打字错误,用

curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
不行

如果用于
CURLOPT_POSTFIELDS
的参数是数组,则它必须是一个关联数组,其中是字符串(或可以转换为字符串的值)

在您的例子中,
$fields
是一个数组,其中一些值是数组。这将不起作用(并引发警告“数组到字符串转换”)


设置一个标头,指定随请求发送的数据为JSON格式

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
如果您想通过
POST
请求将数据以JSON的形式发送到
$fields
中,您可以这样做:

$fields = array( /* ... */ );

$json_fields = json_encode( $fields );

$curl = curl_init( 'http://localhost:8080/endpoint' );

curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $json_fields );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json',
                                          'Content-Length: ' . strlen($json_fields) ] );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );

$response = curl_exec( $curl );

$response_data = json_decode( $response );

如果仍然需要修复某些问题,请按步骤进行调试以隔离问题:

// 1 - Check curl error

$errnum = curl_errno( $curl );
$errstr = curl_strerror( $errnum );

echo "Error: $errnum - $errstr\n";

// 2 - Check http status code of the response

$status = curl_getinfo( $curl, CURLINFO_HTTP_CODE );

echo "Status: $status\n";

// 3 - Inspect the response before coverting to JSON

echo "Response:\n";
var_export( $response );
echo "\n";

尝试分离
json_encode
curl_exec
并调试这两个变量。您可以使用
Try{}catch(异常$e){trigger_error(sprintf('curl失败,错误为#%d:%s',$e->getCode(),$e->getMessage e USER_error)}
并尝试此
$response=curl_exec($curl)
使用
curl\u exec时,您不需要
json\u encode
,很抱歉,我也应该添加帖子正文。我想我需要json_编码,以便能够将$fields转换为post正文的json。我会试试你们提到的,并告诉你们我得到了什么。谢谢。你没有试过卷曲错误()
?迪勒克,那没用。如果您知道该命令或阅读手册,您会看到“成功时返回TRUE,失败时返回FALSE”。它不会抛出异常,因此尝试和捕获在这里没有用。不知何故,我没有复制编码行,这是我的错$postfields是我的json编码变量($postfields=json_encode($fields);)。我在stackoverflow上的帖子有错误,但我已经有了,对不起。我可能会尝试使用python或其他方法来执行这些调用。当邮递员工作得很好的时候,我不应该有这样的困难。不过谢谢,在我转到其他测试之前,我将尝试您的调试部分。