I'm trying to pull some data from twitter via PHP. I'm using the tmhOAuth plugin, which can be found here. https://github.com/themattharris/tmhOAuth/
I wrote my code based off the example file "streaming.php", which can also be found on the above github page. Here is my code:
require 'tmhOAuth.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxxhiddenxxx',
'consumer_secret' => 'xxxhiddenxxx',
'user_token' => 'xxxhiddenxxx',
'user_secret' => 'xxxhiddenxxx'
));
$method = 'http://stream.twitter.com/1/statuses/filter.json';
$params = array(
'follow' => '1307392917',
'count' => '5'
);
$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
$tmhOAuth->pr($tmhOAuth);
That was not printing out any of the twitter data I wanted to pull, and was only showing the debug information that the pr() command writes.
While trying to debug why I wasn't getting any data, I went in and added a line to tmhOAuth.php so that I could see what error cURL was giving. I did this by using
echo curl_error($C);
The error that cURL outputed was :
transfer closed with outstanding read data remaining
I've done some research on that error, but I can't find anything that helps. There were a couple things that I found regarding content-length, but when I dug into the code I saw that the author of tmhOAuth had already addressed those issues (and commenting out his fixes didn't help).
Any help?
Update 1 Here is the response info gathered using curl_getinfo:
//Removed - an updated version is below
Update 2 Thanks to the comments below I realized that twitter was sending me data with transfer-encoding: chunked. I put this line into tmhOAuth.php to force out chunked data:
curl_setopt($c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
That worked, somewhat. I'm no longer getting any cURL errors, but my WRITEFUNCTION callback is still never getting called - so I'm never getting any actual data. Here's the output of my cURL object again:
[response] => Array
(
[content-length] => 0
[headers] => Array
(
[content_type] => text/html; charset=iso-8859-1
[server] => Jetty(6.1.25)
)
[code] => 416
[response] => 1
[info] => Array
(
[url] => http://stream.twitter.com/1/statuses/filter.json
[content_type] => text/html; charset=iso-8859-1
[http_code] => 416
[header_size] => 116
[request_size] => 532
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.118553
[namelookup_time] => 0.043927
[connect_time] => 0.070477
[pretransfer_time] => 0.07049
[size_upload] => 25
[size_download] => 0
[speed_download] => 0
[speed_upload] => 210
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0.118384
[redirect_time] => 0
[request_header] => POST /1/statuses/filter.json HTTP/1.0
User-Agent: themattharris' HTTP Client
Host: stream.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="xxxhiddenxxx", oauth_nonce="xxxhidden", oauth_signature="xxxhidden", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1308226585", oauth_token="xxxhiddenxxx", oauth_version="1.0"
Content-Length: 25
Content-Type: application/x-www-form-urlencoded
)
)
)
Update 3: Couple things I've figured out so far... I removed the 'count' parameters from my POST request, and now the page seems to take forever. I figured this meant it was just downloading tons and tons of data, so I put a break into the streaming callback function, setup so that it kills the page after 5 loops.
I did this, and let it sit for quite awhile. After about 5 minutes, the page finished loading, and showed me what data I had gathered. It looked like I had gotten no data each time it ran through - only an end of line character. So, it's taking a minute for every piece of data I am downloading, and even then the only data that shows is an end of line character. Weird? Is this a twitter issue or a cURL issue?
I tried with the token api but never got something good, so this is the script I found here :
<?php
/**
* API Streaming for Twitter.
*
* #author Loïc Gerbaud <gerbaudloic#gmail.com>
* #version 0.1 "itjustworks"
*/
define('TWITTER_LOGIN','login'); //login twitter
define('TWITTER_PASSWORD','myp4ssw0rd'); //password twitter
$sTrackingList = 504443371;//read my account but could be keywords
// ?
while(1){
echo 'Connexion ';
read_the_stream($sTrackingList);
echo 'Deconnexion ';
}
/**read the stream
*
*/
function read_the_stream($sTrackingList){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://stream.twitter.com/1/statuses/filter.json');
curl_setopt($ch,CURLOPT_USERPWD,TWITTER_LOGIN.':'.TWITTER_PASSWORD);//Le couple login:password
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Twitter-Client: ItsMe','X-Twitter-Client-Version: 0.1','X-Twitter-Client-URL: http://blog.loicg.net/'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=".$sTrackingList);//read the doc for your request
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_callback');//function callback
curl_exec($ch);
curl_close($ch);
}
/** a demo with a writting log or put in MySQL
*/
function write_callback($ch, $data) {
if(strlen($data)>2){
$oData = json_decode($data);
if(isset($oData->text)){
file_put_contents('log',$oData->text."\n",FILE_APPEND);
}
}
return strlen($data);
}
?>
run this script in your browser (you can close it after), update your twitter account and check the .log
After about 5 minutes, the page finished loading
Are you running streaming.php in the browser? If so, you have to run it via ssl, otherwise it doesn't work. I have a server chron job pointing to the file but you can do it also with the terminal:
php /path/to/here/streaming.php
For view the data you are getting, you can store it into a database or log:
function my_streaming_callback($data, $length, $metrics) {
$ddf = fopen('/twitter/mydata.log','a');
fwrite($ddf,$data);
fclose($ddf);
}
Related
I am calling a REST endpoint in PHP using cURL to fetch some JSON data:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
It takes 2.5 seconds to fetch the data using the above code on my localhost. The same code takes around 7.5 seconds when run on the live server. When the URL is opened directly on a browser it takes only 1.5 seconds.
My question is: Why does it take so long for cURL to fetch data on the live server and how can I solve this problem?
Below is the output of curl_getinfo($ch) on the server:
Array
(
[content_type] => application/json
[http_code] => 200
[header_size] => 420
[request_size] => 113
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 7.305496
[namelookup_time] => 0.150378
[connect_time] => 0.473187
[pretransfer_time] => 0.473237
[size_upload] => 0
[size_download] => 1291504
[speed_download] => 176785
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 1.787901
[redirect_time] => 0
[redirect_url] =>
[certinfo] => Array
(
)
[primary_port] => 80
[local_port] => 53962
)
I found the solution to my problem. As I had mentioned in the question, the service was loading the fastest in browsers. So, I checked the 'Request Headers' of the request in the 'Network' tab of Google Chrome Inspector. I copied those headers and used them in my cURL request in PHP. After scraping those headers I found that all I needed to do was to add an Accept-Encoding header. I passed a value of gzip like so:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
but setting it to an empty string also works.
curl_setopt($ch, CURLOPT_ENCODING, '');
According to the php.net manual for CURLOPT_ENCODING:
The contents of the "Accept-Encoding: " header. This enables decoding
of the response. Supported encodings are "identity", "deflate", and
"gzip". If an empty string, "", is set, a header containing all
supported encoding types is sent.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "set ur url");
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Please check this example
I am trying to create a page to log in to a local router automatically. I am using CURL currently to log in to the page and authenticate. This part of the the code appears to be working correctly. The issue I am having is that once CURL has authenticated, I need to then redirect the user to this page so that they can navigate, however, I will also need to use the cookies collected by CURL.
Here is my code as it stands at the moment
$data = array(
'username' => 'admin',
'password' => 'admin',
);
$ch = #curl_init();
curl_setopt($ch, CURLOPT_URL,'http://192.168.69.1:65080/login.cgi');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'public_html/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'public_html/cookie.txt');
$result = curl_exec ($ch);
$info = curl_getinfo($ch);
curl_close ($ch);
print_r($result);
print_r($info);
//Working until this point
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
foreach($cookies as $key=>$cookie)
{
setcookie($key, $cookie, time() + 60*60*24*30, '/');
}
header("location:".$info['redirect_url']);
As you can see I found a snippet to loop through the $result info and then set them as cookies before redirecting, however, this is not working correctly and I am redirected to the login page not the index page.
If I do a further call before I close CURL, using the redirect url as the url, I do get a partial print of the index page, however, the important images etc are not displayed. But I need to be able to access the page and navigate rather than simply printing the page.
Here is a print of $result
HTTP/1.1 302 Found
Location: /index.cgi
Set-cookie: show_security_warning=deleted; expires=Sunday, 09-Jun-13 10:54:00 GMT
Set-cookie: ui_language=en_US; expires=Tuesday, 19-Jan-38 03:14:07 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Date: Mon, 09 Jun 2014 10:54:01 GMT
Server: lighttpd/1.4.31
Here is a print of $info
Array
(
[url] => http://192.168.69.1:65080/login.cgi
[content_type] => text/html
[http_code] => 302
[header_size] => 314
[request_size] => 251
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.484
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 255
[size_download] => 0
[speed_download] => 0
[speed_upload] => 526
[download_content_length] => -1
[upload_content_length] => 255
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] => http://192.168.69.1:65080/index.cgi
)
Here is my cookie.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
192.168.69.1 FALSE / FALSE 0 AIROS_SESSIONID d19e097a07b7b76fd7d90267a8e1f4d2
192.168.69.1 FALSE / FALSE 1370775278 show_security_warning deleted
192.168.69.1 FALSE / FALSE 2147483647 ui_language en_US
Finally here is a print of $cookies
Array
(
[show_security_warning] => deleted
)
If anyone can point me in the right direction of how to achieve the next step, I would be most grateful.
I'm not sure your strategy will ever be sucessfull.
Curl is working as a web client. It means Curl and your web browser are probably seen as distinct hosts by the router.
[CLIENT (WEB BROWSER)] ---HTTP---> [PHP WEBSERVER]
[CURL] ---HTTP---> [ROUTER (WEBSERVER)]
PHP has a particular behaviour : it stores sessions in files whose name depends on the session id cookie value only, so it is (or was ... I dont know all versions of PHP) possible to steal a session by capturing the session cookie / cloning the cookie values.
Not all CGI libs are doing the same. I believe your router has a safer session storage method, as it should be expected from a security dedicated device (for example
a key based on the client IP and the session cookie value).
In this case your method is useless.
You'd better to use a javascript based form (in order to post the id/password) and maybe an iframe requesting the router login page before (in order to initialize the routers cookie values). Using a javascript form will show the credentials to your user which is probably not what you want
I'm trying to run an URL (which have signout functionality) through the CURL. But it is returning 302 http code. Same url when i run through the POSTMAN ( Google Chrome addon ) or POSTER ( Firefox Addon) , then it is return proper result ( {"status" : "success" } ). Any help would be greatly appreciated.
URL (JAVA APPLICATION URL) : http://website.mywebsite.com:8083/VideoBook/signout.action
MY CODE :
// Open log file
$logfh = fopen("GeoserverPHP.log", 'w') or die("can't open log file");
// Initiate cURL session
$service = "http://website.mywebsite.com:8083/VideoBook/";
$request = "signout.action";
$url = $service . $request;
$ch = curl_init($url);
// Optional settings for debugging
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $logfh);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, true);
//Required GET request settings
// $passwordStr = "geosolutions:Geos";
// curl_setopt($ch, CURLOPT_USERPWD, $passwordStr);
//GET data
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
//GET return code
$successCode = 200;
$buffer = curl_exec($ch);
echo "CURL INFO : <BR/> " ;
print_r(curl_getinfo($ch));
echo "CURL OUTPUT : <BR/> " ;
print_r($buffer);
// Check for errors and process results
$info = curl_getinfo($ch);
if ($info['http_code'] != $successCode) {
$msgStr = "# Unsuccessful cURL request to ";
$msgStr .= $url." [". $info['http_code']. "]\n";
fwrite($logfh, $msgStr);
} else {
$msgStr = "# Successful cURL request to ".$url."\n";
fwrite($logfh, $msgStr);
}
fwrite($logfh, $buffer."\n");
curl_close($ch);
fclose($logfh);
OUTPUT IN BROWSER :
CURL INFO :
Array
(
[url] => http://website.mywebsite.com:8083/VideoBook/signout.action
[content_type] =>
[http_code] => 302
[header_size] => 254
[request_size] => 105
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.58976
[namelookup_time] => 0.004162
[connect_time] => 0.297276
[pretransfer_time] => 0.297328
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.589739
[redirect_time] => 0
[redirect_url] => https://hpecp.mywebsite.com:8443/cas/login?service=http%3A%2F%2Fwebsite.mywebsite.com%3A8083%2FVideoBook%2Flogin.action
[primary_ip] => 125.21.227.2
[certinfo] => Array
(
)
[primary_port] => 8083
[local_ip] => 10.0.0.8
[local_port] => 50710
)
CURL OUTPUT :
LOG File Details :
* Hostname was NOT found in DNS cache
* Trying 125.21.227.2...
* Connected to website.mywebsite.com (125.21.227.2) port 8083 (#0)
> GET /VideoBook/signout.action HTTP/1.1
Host: website.mywebsite.com:8083
Accept: application/json
< HTTP/1.1 302 Moved Temporarily
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Location: https://hpecp.mywebsite.com:8443/cas/login?service=http%3A%2F%2Fwebsite.mywebsite.com%3A8083%2FVideoBook%2Flogin.action
< Content-Length: 0
< Date: Tue, 20 May 2014 06:02:29 GMT
<
* Connection #0 to host website.mywebsite.com left intact
* Issue another request to this URL: 'https://hpecp.mywebsite.com:8443/cas/login?service=http%3A%2F%2Fwebsite.mywebsite.com%3A8083%2FVideoBook%2Flogin.action'
* Hostname was NOT found in DNS cache
* Trying 15.126.214.121...
* Connected to hpecp.mywebsite.com (15.126.214.121) port 8443 (#1)
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* Unknown SSL protocol error in connection to hpecp.mywebsite.com:8443
* Closing connection 1
# Unsuccessful cURL request to http://website.mywebsite.com:8083/VideoBook/signout.action [302]
try to add ssl verify false and follow location and now all set
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//output:-
CURL INFO :
Array ( [url] => https://exampl.com:8443/cas/login?service=http%3A%2F%2Fexample%3A8083%2FVideoBook%2Flogin.action [content_type] => text/html;charset=UTF-8 [http_code] => 200 [header_size] => 593 [request_size] => 273 [filetime] => -1 [ssl_verify_result] => 18 [redirect_count] => 1 [total_time] => 3.073 [namelookup_time] => 0 [connect_time] => 0.577 [pretransfer_time] => 1.794 [size_upload] => 0 [size_download] => 8003 [speed_download] => 2604 [speed_upload] => 0 [download_content_length] => 8003 [upload_content_length] => -1 [starttransfer_time] => 2.387 [redirect_time] => 0.686 )
You so need to check auth credentials on your end
I think, adding these three parameter CURLOPT_REFERER, CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE and an valid cookie file can solve this. I didn't tested the code.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Do the job.
In order to log out of any kind of session, you first need to be logged in, so the service must be expecting some reference to an existing session.
Either it expects you to give it information about which user should be logged out, or it is intended to log your script out after a series of calls to other services.
What it cannot do is automatically log out the user who is accessing your page, because it has no way of seeing them. The request originates entirely on your server, and only contains the information you pass to it with CURL. Nor will you be able to give it the information a browser would have, unless your script is on the same domain, as the browser will not pass your script the cookies set by the other site.
I've looked at the prior posts about cURL and HTTP code 0, but they aren't helping.
I can cURL into www.bambooping.com with script below from localhost - ie, test_curl.php on localhost calls test_curl2.php on bambooping.com. However, if I run it on bambooping.com, I get HTTP code 0. (I know calling this on same host is dumb - it's just to isolate problem.)
On bambooping.com safe_mode is not set, and curl is compiled in (ie, should be since I can cURL in). This is very strange - the calling host is preventing the cURL. Why would calling out with cURL fail like this, yet calling into that same host with cURL be ok?
test_curl.php:
<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
// curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
// make it blank - then it is ignored - otherwise, checked and error returned!
curl_setopt($ch, CURLOPT_REFERER, '');
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Download the given URL, and return output
$output = curl_exec($ch);
print_r(curl_getinfo($ch));
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
$str = curl_download("http://www.bambooping.com/test_curl2.php");
echo $str;
?>
test_curl2.php
<?php
echo "I am here";
?>
The curl_getinfo is:
Array
(
[url] => http://www.bambooping.com/test_curl2.php
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 4.3E-5
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
)
Ideas? I'm fresh out...
Thanks -
Please check there is a curl error
it's say the problem to you
<?php
if(curl_errno($ch)) echo 'Curl error: ' . curl_error($ch);
?>
The server for www.bambooping.com is probably sitting behind a firewall that prevents outgoing HTTP requests. Even though it's the same server, the request still needs to go out into the wild to resolve the DNS.
You could either edit the hosts file on your server to include 127.0.0.1 www.bampooing.com. Or you could change the URL to http://127.0.0.1/test_curl2.php, as this localhost domain is probably not blocked by the firewall.
I am building a website that makes standard HTTP calls to an API. My first call is a straight-forward GET with no parameters using basic auth. I am using Curl in my php. I am running this via a local install of XAMPP. My call is not working but if i have a colleague run the php on his Linux box running an older version of ubuntu PHP it works fine. What is the best way to troubleshoot this issue? My guess is it is something with my XAMPP install but is there a good method for troubleshooting? I have used curl_getinfo on my curl session to get the return values and it doesn't seem to provide much insight as far as I can tell.
Here is the curl_getinfo output:
Array (
[url] => https://www.ebusservices.net/webservices/hrpcertws/rbs/api/merchants/267811683882/consumers.xml?
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 107
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.28
[namelookup_time] => 0.015
[connect_time] => 0.015
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array ( )
[primary_ip] => 127.0.0.1
[primary_port] => 8888
[local_ip] => 127.0.0.1
[local_port] => 59509
[redirect_url] =>
)
I am using:
XAMPP 1.8.1
PHP Version 5.4.7
cURL 7.24.0
on Windows 7
Added Code:
<?php
error_reporting(E_ALL);
$session = 'FALSE';
// Initialize the session
$session = curl_init();
$stderr = fopen("curl.txt", "w+");
// Set curl options
curl_setopt($session, CURLOPT_URL, 'https://www.ebusservices.net/webservices/hrpcertws/rbs/api/merchants/12233442/consumers.xml?');
curl_setopt($session, CURLOPT_STDERR, $stderr);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, "username:pwd");
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($session, CURLOPT_SSLVERSION, 3);
curl_setopt($session, CURLOPT_VERBOSE, 1);
// Make the request
$response = curl_exec($session);
print_r(curl_getinfo($session));
// Close the curl session
curl_close($session);
fclose($stderr);
// Get HTTP Status code from the response
$status_code = array();
preg_match('/\d\d\d/', $response, $status_code);
// Check the HTTP Status code
if(isset($status_code[0]))
{
switch( $status_code[0] )
{
case 100:
break;
case 200:
break;
case 503:
die('Your call to HRP API failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');
break;
case 403:
die('Your call to HRP API failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
break;
case 400:
die('Your call to HRP API failed and returned an HTTP status of 400. That means: Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
break;
case 401:
die('Your call to HRP API failed and returned an HTTP status of 401. That means: Unauthorized. The credentials supplied do not have permission to access this resource.');
break;
case 404:
die('Page not found.');
break;
default:
die('Your call to HRP API returned an unexpected HTTP status of:' . $status_code[0]);
}
}
else
{
echo 'failed';
}
// Get the XML from the response, bypassing the header
if (!($xml = strstr($response, '<?xml'))) {
$xml = null;
//echo 'in xml';
}
// Output the XML
echo htmlspecialchars($xml, ENT_QUOTES);
?>
Try using Fiddler to see exactly what is in the HTTP traffic.