in adobe connect api there is 2 step for getting data:
step 1) from http://87.107.152.107/api/xml?action=login&login=username&password=password I have to get token to verify in other api.
step 2) from http://87.107.152.107/api/xml?action=report-my-meetings I can get meetings report with token got from step 1.
the problem is when I use postman to use these api , postman set cookie from step 1 api. and it needs this cookie for step 2.
I want to use cookie in curl php but I don't know how to get it. My code for step 1:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://87.107.152.107/api/xml?action=login&login=username&password=password',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
I used this and worked for me:
curl_setopt($ch, CURLOPT_HEADER, 1);
//Return everything
$res = curl_exec($ch);
//Split into lines
$lines = explode("\n", $res);
$headers = array();
$body = "";
foreach($lines as $num => $line){
$l = str_replace("\r", "", $line);
//Empty line indicates the start of the message body and end of headers
if(trim($l) == ""){
$headers = array_slice($lines, 0, $num);
$body = $lines[$num + 1];
//Pull only cookies out of the headers
$cookies = preg_grep('/^Set-Cookie:/', $headers);
break;
}
}
Related
Hi I'm trying to setup a PHP CURL call using OAuth1 authorization method.
I've tried with POSTMAN 1st, to generate the PHP code. I've completed it with the necessary datas
<?php
$conskey = 'XXXXXXX';
$conssec = 'XXXXXXX';
$nonce = mt_rand();
$timestamp = time();
$url = 'https://some-website/api/project/subproject';
$method = 'POST';
$oauth = new OAuth($conskey, $conssec, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->setNonce($nonce);
$oauth->setTimestamp($timestamp);
$signatureOAuth = $oauth->generateSignature($method, $url);
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_URL => $url.'?oauth_consumer_key='.$conskey.
'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='.$timestamp.
'&oauth_nonce='.$nonce.
'&oauth_version=1.0&oauth_signature='.$signatureOAuth,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $jsonDatas,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
// THE COOKIE I WANNA GET
'Cookie: SSESSd4f3e89d4699e1d1a071aa37eab4fcEd=DWS4UqpaykI2y7q-HJXEzGN82AKHQYnWo5hbsqkAqiQ'
),
));
$result = curl_exec($curl);
curl_close($curl);
But I've noticed that there's the cookie in the CURLOPT_HTTPHEADER entry but I don't have any idea how POSTMAN generate this cookie.
Without this cookie or with a dumb string, the CURL response is always Invalid Signature
Postman is not generating cookies for you, and neither is curl/php. you either did some prior requests (usually GET to get cookies) requests to the website where you received some cookies, or gave the cookies to postman some other way.
since you're not showing us the real url, we can only speculate, but to take an example, here is how to get a session cookie for stackoverflow.com:
<?php
$ch=curl_init();
curl_setopt_array($ch,array(
CURLOPT_COOKIEFILE => "", // setting it to empty activates the curl cookie engine (its disabled by default.),
CURLOPT_URL => "https://stackoverflow.com/",
CURLOPT_RETURNTRANSFER => true,
));
$html=curl_exec($ch);
$cookies = (function($cookielist):array{
$cookies = array();
foreach($cookielist as $cookie_raw) {
$chunks = explode("\t", $cookie_raw);
//var_dump($chunks);
$cookie['domain'] = $chunks[0];
$cookie['secure'] = $chunks[1];
$cookie['path'] = $chunks[2];
$cookie['???todo_wtf_is_this'] = $chunks[3];
$cookie['expiration'] = $chunks[4];
$cookie['name'] = $chunks[5];
$cookie['value'] = $chunks[6];
$cookies[] = $cookie;
}
return $cookies;
})(curl_getinfo($ch, CURLINFO_COOKIELIST));
var_export($cookies);
prints something like
array (
0 =>
array (
'domain' => '#HttpOnly_.stackoverflow.com',
'secure' => 'TRUE',
'path' => '/',
'???todo_wtf_is_this' => 'FALSE',
'expiration' => '2682374400',
'name' => 'prov',
'value' => '9c06f038-9f70-bee8-2a64-b095656175d1',
),
)
which could be used like
// THE COOKIE I WANNA GENERATE
'Cookie: '. $cookies[0]["name"].'='.$cookies[0]["value"]
but this is very rarely done, usually you'd just use curl's built-in cookie engine to handle cookies automatically..
I'm very new to PHP and API, and my company was asked for a job of incorporating API calls on a PHP website.
This is the info about the API that the client gave to us:
API Manual
General Notes:
API URL: https://www.somedomain.com/api/
apiKey: 123qwe456rty678yui
HTTP Verb: POST
Content-Type: application/x-www-form-urlencoded
1. {EMPLOYEES category}
POST /en/1001/cat/10
POST /en/1001/cat/10/page/[999]
Observations:
• the list returns 20 records
• to get the reamining records: /en/1001/cat/10/page/2; /en/1001/cat/10/page/3; ...
• the information is returned in JSON format
• all the requests should be made by the POST method and the api key should be sent on the request body
Input variables:
• apiKey => App key
Return parameters:
retCode = “error”
retCode = “ok”
(...)
2. {NEWS category}
POST /en/2002/cat/20
POST /en/2002/cat/20/page/[999]
Observations:
• the list returns 20 records
• to get the reamining records: /en/2002/cat/20/page/2; /en/2002/cat/20/page/3; ...
• the information is returned in JSON format
Input variables:
• apiKey => App key
Return parameters:
retCode = “error”
retCode = “ok”
(...)
And I've made a simple PHP script but I'm getting an empty return value:
<?php
//API URL: https://www.somedomain.com/api/
//This is to test the Employees category - /en/1001/cat/10
$curl = curl_init();
$apiKey = urlencode("123qwe456rty678yui");
//$apiKey = "123qwe456rty678yui";
//it doesn't matter if this variable is urlencoded or not, as the result is always empty...
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.somedomain.com/api/en/1001/cat/10",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_SSL_VERIFYPEER => "false",
CURLOPT_POSTFIELDS => "apiKey=$apiKey",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "Response: " . $response;
}
?>
Like I said, I'm very new to this, PHP and API's, and the client isn't very helpful.
Based on the Manual API I have, what I'm doing wrong? $response is always empty, it gives no error even if I enter a wrong API Key, and I don't even know if I'm calling this the correct way...
Thanks in advance!
Here is a function that should work from the manual: https://www.php.net/manual/en/function.curl-setopt-array.php#89850
<?php
function get_web_page($url, $curl_data )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $curl_data, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1 //
);
$ch = curl_init($url);
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch) ;
$header = curl_getinfo($ch);
curl_close($ch);
// $header['errno'] = $err;
// $header['errmsg'] = $errmsg;
// $header['content'] = $content;
return $header;
}
$curl_data = "var1=60&var2=test";
$url = "https://www.example.com";
$response = get_web_page($url,$curl_data);
print '<pre>';
print_r($response);
?>
You can also try the file_get_contents function to get this JSON data.
Something like this could do the job:
// API URL
$apiUrl = 'https://www.somedomain.com/api/en/2002/cat/20';
// Request options
$opts = [
"http" => [
'method' => 'POST',
'header' => [
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 123qwe456rty678yui'
]
]
];
// Request context
$context = stream_context_create($opts);
// Get JSON
$json = file_get_contents($apiUrl, false, $context);
Try and see if you can adapt it.
I am trying to automate the sign in process with the Zoom API to allow me to use my website to create meetings but I cannot seem to get the sign in to work in order to allow me to get an authentication code to call the API methods. I am doing this in PHP and I am fairly novice so may be making some fundamental errors.
If I make the 2 calls in the code below via postman and copy the code from postman to my PHP then it works for an hour (My guess is that this is because the cookies have an hour expiry?). Given I thought it was down to cookies I have tried a couple of way to copy the cookies returned in the set-cookie header but this still isn't working. Can someone help me with what I am missing? Apologies if the code is messy...
<?php
$curl = curl_init();
$headers = [];
curl_setopt_array($curl, array(
CURLOPT_URL => "https://zoom.us/oauth/v2/signin",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HEADERFUNCTION => function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
},
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('email' => 'some#thing.com','password' => 'somePassword','client_id' => 'someClientID','redirect_uri' => 'https://www.some.thing','response_type' => 'code','scope' => '','state' => ''),
));
$response = curl_exec($curl);
$cookie2 = "Cookie: ";
echo "<br>_____<br>";
print_r($headers["set-cookie"]);
echo "<br>_____<br>";
//echo var_dump(curl_getinfo($curl));
foreach ($headers["set-cookie"] as &$value) {
echo "<br>------------<br>";
echo $value;
$cookies = explode(';', $value);
$cookie2 .= $cookies[0] . "; ";
}
unset($value);
echo "<br>_____<br>";
echo $cookie2;
echo "<br>_____<br>";
echo $response;
$nextUrl = json_decode($response, true)["nextUrl"];
// -------------------------------------------
//$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://zoom.us/oauth/authorize?client_id=clientID&response_type=code&redirect_uri=https://www.some.thing",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
$cookie2
),
));
$response = curl_exec($curl);
curl_close($curl);
$pieces = explode("> ", $response);
$authCode = substr($pieces[1], 0, -3);
echo "auth code: " . $authCode;
// ---------------------------------------------------------------
?>
My Solution for this was to use a refresh token call instead to avoid the need for signing in for every call. Now for each call to the Zoom API I refresh the token and then make the API call, storing the tokens on the server for future use.
Besides refreshing your token for each request, you can generate immortal tokens with your own client secret on the jwt token site. For example, I produced a valid token until 2052 and can request as many requests as I want.
Firstly, you should take your token from Zoom and open jwt.io web site and paste it here. You can change the timezone, exp: expiring time, iat: starting time
Finally, you pass your client_secret key
I've been trying all morning to get an XML string uploaded via an API so that it submits my order but no matter what I try it simply isn't working for me.
My URL:
$url = "http://example.com/SubmitOrder?apiKey=ABC123&clientID=MYId&orderXml=".$xml;
$xml is my xml details already pre-formatted.
I then put this into my curl section:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$resp = curl_exec($curl);
$xml1=simplexml_load_string($resp) or die("Error: Cannot create object");
print_r($xml1);
echo "Submitted";
The response I get is "Error: Cannot create object" and I can see that my details have not been submitted.
Where am iI going wrong ??
Many thanks.
You can check your response data and create data in this code example.
<?php
$url = 'https://www.w3schools.com/xml/note.xml';
$resp = file_get_contents($url);
if (resp) {
$string = simplexml_load_string($resp);
var_dump($movies);
}
but if you want get data in method curl try this.
<?php
$html_brand = 'https://www.w3schools.com/xml/note.xml';
$ch = curl_init();
$options = array(
CURLOPT_URL => $html_brand,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200: # OK
break;
default:
echo 'Status code HTTP: ', $http_code, "\n";
}
var_dump($response);
curl_close($ch);
die();
Check your data. And check your response status code.
Using postman, I send a POST to (my username and password are filled in):
https://ssl.reddit.com/api/login?api_type=json&user=XXX&passwd=XXX&rem=True
I receive a response containing a modhash and a cookie. Then, I send a second POST with postman to:
https://en.reddit.com/api/comment?api_type=json&text=7/1/15TEST&thing_id=t1_csa56v2
with the following headers (XXX has been confirmed and filled in):
User-Agent: XXX
Cookie: reddit_session=XXX
X-Modhash: XXX
This provides the correct response, but when I try to do the same thing with CURL in my PHP, it responds with USER_REQUIRED. Once again, I have confirmed that the cookie and modhash are correct.
$name = 't1_csa56v2';
$text = 'NEWEST TEST 7/2/15 12:20am';
$url = 'https://en.reddit.com/api/comment';
$modhash = 'XXX';
$cookie = 'XXX';
$headerFields = array (
'User-Agent' => 'XXX',
'Cookie' => 'reddit_session='.$cookie,
'X-Modhash' => $modhash
);
$postFields = array (
'api_type' => 'json',
'text' => $text,
'thing_id' => $name
);
$field_string = http_build_query($postFields);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $field_string);
$response = curl_exec($ch);
What am I doing wrong? Why can't I get the same response?
Screenshot of POSTMAN:
<?php
error_reporting(E_ALL);
$name = 't1_csa56v2';
$text = 'NEWEST TEST 7/2/15 12:20am';
$url = 'https://en.reddit.com/api/comment';
$modhash = 'XXX';
$cookie = 'XXX';
$headerFields = array (
'X-Modhash' => $modhash
);
$postFields = array (
'api_type' => 'json',
'text' => $text,
'thing_id' => $name
);
$ch = curl_init($url);
assert(curl_setopt_array($ch,
array(
CURLOPT_AUTOREFERER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FORBID_REUSE => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 11,
CURLOPT_ENCODING=>"",
CURLOPT_USERAGENT=>'XXX',
CURLOPT_COOKIE=>'reddit_session='.$cookie,
CURLOPT_HTTPHEADER=>$headerFields,
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$postFields,
)));
$response = curl_exec($ch);
try this.. not sure exactly what you do wrong, but user agent should be set with CURLOPT_USERAGENT , and the cookie should be set with CURLOPT_COOKIE and you should let curl encode it for you, rather than using http_build_query
, and you should explicitly set it to a POST request, as its a GET request by default. should also enable E_ALL error reporting