2017-01-25 22 views
0

我这里有一个卷曲的代码如下所示:如何在PHP中使用curl来访问curl中的某些数据?

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

我想写一个php文件的代码。我不知道如何开始编写这段代码。任何人都可以告诉如何在PHP中编写curl代码?

+0

一个好的起点是curl_init()中的[PHP.net文档,覆盖CURL](http://php.net/manual/en/ref.curl.php) – Tom

回答

0

首先使用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)); 
1

试试这个:

$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 
} 

Reference

1

您需要使用PHP Client URL Library

解决方案为您的代码:

<?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); 

API的响应是$return变量。

+0

这个值被添加到里面? – johanna

+0

@johanna这是一个页面的网址。 –

+0

是那个url常量? – lena