如何在php中编写一个curl来访问php中的一些数据?

如何在php中编写一个curl来访问php中的一些数据?,php,Php,我有一个curl代码,如下所示: curl --request GET \ --user 60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR:API-P4ss \ https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mG

我有一个curl代码,如下所示:

curl --request GET \
--user 60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR:API-P4ss \
https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2

我想在php文件中编写此代码。我不知道如何开始写这段代码。有人能告诉我如何用php编写curl代码吗?

首先使用file\u get\u contents php函数

如果找不到解决方案,请尝试以下代码

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

首先使用file_get_contents php函数

如果找不到解决方案,请尝试以下代码

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));
试试这个:

$url = 'API URL';

try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $APIresponse = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($APIresponse, true);
    // $res contains the API response as an array
}
catch(Exception $e)
{
    // Error mesage
}
试试这个:

$url = 'API URL';

try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $APIresponse = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($APIresponse, true);
    // $res contains the API response as an array
}
catch(Exception $e)
{
    // Error mesage
}
您需要使用

代码的解决方案:

<?php
$username = "60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR";
$password = "API-P4ss";
$process = curl_init("https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
var_dump($return);
您需要使用

代码的解决方案:

<?php
$username = "60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR";
$password = "API-P4ss";
$process = curl_init("https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
var_dump($return);

一个好的起点应该是curl_init()中添加了哪个值?@johanna这是指向页面的url。url是常量吗?输出如下所示:string(66)“HTTP/1.1400坏_请求内容长度:0连接:Close”有人能回答我的问题吗?在curl_init()中正在添加哪个值?@johanna这是指向页面的url。该url是否为常量?输出如下所示:字符串(66)“HTTP/1.1 400错误请求内容长度:0连接:关闭”有人能回答我的问题吗?