How to encode token using curl - php

STEP - 1
I am sending a xml request using CURL and its returning "TOKEN" to me. Please find the below code here:
<root><request><type>mykey</type><username>myusername</username><password>mypassword</password></request></root>
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);// passing API URL here
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $xml_strign); // passing xml as a string
$result = curl_exec ($curl_handle);
In this above code executing fine and returning Token as a result.
STEP - 2
In second step, I have to send above token to get my result but not getting result.Please find the below code:
$xml = "<root>
<request>
<type>myparam</type>
<token>lGcvdOnetuxK0paE+AIxE93GB85DURIpOeoBw8quqOs=</token>
</request>
</root>";
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $xml);
$result = curl_exec ($curl_handle);
curl_close ($curl_handle);
print_r($result);
May be this Token is contains special charter as i guess, so that I am not getting result. I have seen console url response status in browser its 200 ok.
I do appreciate advance for your help.

I think you have a problem with your post data. You should read details about CURLOPT_POSTFIELDS here :
http://php.net/manual/fr/function.curl-setopt.php
So your xml variable should probably be defined like this:
$xml = array("token" => "lGcvdOnetuxK0paE+AIxE93GB85DURIpOeoBw8quqOs");
At least it should be an array.

Related

YouTube v3 API call for search.list: proper curl setting

I am sending a call to the YouTube API using search.list (retrieve the first search result for a keyword) but I am getting nothing in return.
Here is my call:
$api_url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&type=video&key=MYKEY&&format=json&q=SEARCHTERMS;
Note: MYKEY is my API key I got from Google (currently active) and SEARCHTERMS is any word to search.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
echo $value;
The URL works correctly (tested on browser) and I'm not getting any kind of error (console), but still this curl request isn't echoing any data from YouTube. Isn't it properly set?
P.s. My quota isn't exceeded.
SOLVED!
Watch out: youtube API doesn't read spaces in url (eg. in search terms), so you gotta streplace all of them with "+"

Parsing XML Response with xml schema

Edit
The code given below will work in localhost as it is, if anyone want to copy and try it.The given credentials are valid.
I have a php code which requests data from an API service. An XML request is sent and in response XML data is recieved. I have stored the respose data in a variable $output. When doing echo $output, the details in the XML response is printed. But now I need to parse this response and store the required data in variables. E.g: I need to save $customer_id = value from the <customer_id>12345</customer_id>. I did a thorough google search and tried all the snippets provided by different developers, but no use.
I tried var_dump(simplexml_load_string($output)); and it is returning object(SimpleXMLElement)#1 (0) { }. I even tried converting the XML data to array.
index.php
<?php
$appId ="MFS149250";
$appPass ="5TEBRPCZ";
$brokeCode ="ARN-149250";
$iin = "5011217983";
$xml_data = '<?xml version="1.0" encoding="UTF-8"?>
<NMFIIService>
<service_request>
<appln_id>'.$appId.'</appln_id>
<password>'.$appPass.'</password>
<broker_code>'.$brokeCode.'</broker_code>
<iin>'.$iin.'</iin>
</service_request>
</NMFIIService>';
$URL = "https://uat.nsenmf.com/NMFIITrxnService/NMFTrxnService/IINDETAILS";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
//echo "<textarea>".$output."</textarea>";
echo $output;
var_dump(simplexml_load_string($output));
curl_close($ch);
?>

PHP Curl GET request doesn't return data

This really has me perplexed and other examples on Stackoverflow have not helped.
If I type into the browser :
https://api.bitfinex.com/v2/book/tXMRUSD/P0
I will get an long array of data which is correct.
With the following simple code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bitfinex.com/v2/book/tXMRUSD/P0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
I get :
bool(false)
The only difference is my browser fetches the data directly, while the PHP code is on a local website hosted using IIS on windows 10.
I've tried everything and just can't see where I'm going wrong. Any help will be most appreciated. (Ironically, all the authenticated and encrypted POST code works fine and I'm stuck on the easy stuff!)
Try This code it will work!!!!
<?php
$url= "https://api.bitfinex.com/v2/book/tXMRUSD/P0";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>

Get api result from URL using PHP

The following URL is from the FobBugz API documentation:
https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2 (you can find it here)
If I copy and paste the above URL into a web browser I get an XML response. What I would like to do is create a function that returns the XML response as a its result.
I am so stuck, it is simply not working. All I seem to get in response is an empty string. When I use this 'example' on the FogBugz site I get XML telling me that I am NOT logged in.
The function below comes mostly from here: Make a HTTPS request through PHP and get response I have been messing with it for hours without success.
function searchBug(){
$data = "https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2";
//echo $data;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
[EDIT: in response to comment]
The response that I want to receive is:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<error code="3">
<![CDATA[ Not logged in ]]>
</error>
</response>
As that is what is shown in my browser when I paste the URL and press enter.
You can do it like below:-
<?php
function searchBug($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$sXML = searchBug('https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2');
header('Content-Type: text/xml');
echo $sXML;
Output:- http://prntscr.com/f5fj44

Can't see XML response from PHP cURL api Call

I'm using a remote API with cURL and querystrings. The issue I'm having is that I can't see the data being returned to me (which is in XML format). To start, here's my code:
$url = 'https://api.somehost.com/GetRequest.do';
$query = 'User=JohnDoe&Password=ABC123&Function=auth';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$curlresponse = curl_exec($ch);
I know the cURL request is working because if I change the password to something incorrect, I get a response back (can see it using print_r($curlresponse) telling me the password is incorrect.
However, when I enter the correct password, I see nothing when I print_r($curlresponse) yet I know I'm receiving the data because a print_r(curl_getinfo($ch)) shows a larghe download_content_length which is the data I'm expecting. The data I'm expecting back is in XML format. I'm just not sure what I'm missing.
Thank you!

Categories