Hello,
I am currently writing a client to access a Microsoft Exchange server and read contacts, appointments etc. from it.
Through days of searching I've been able to connect to the EWS via PHP's Soap client and a custom HTTPS Stream wrapper. This website helped me greatly at this point.
Everything worked fine on my Windows 7 machine using XAMPP
Now I uploaded my project to a Debian 6.0 Squeeze development machine that has exactly the same configuration as my Windows machine regarding the web-server, php settings, mysql settings etc. but it just wont work anymore
The debian machine can resolve and ping the exchange server without problems
I nailed the actual problem down to a point, where cURL isn't able to retrieve the WSDL file of the EWS
It always receives an empty response and a 401 (Unauthorized) status code
The credentials I use are correct, the same credentials work on my windows machine
I extracted the faulty piece of code and tried running it stand-alone, it looks like this:
echo "Trying to get https://".$cfg[ 'Exchange.Server' ]."/EWS/Services.wsdl<br>";
$curl = curl_init( 'https://'.$cfg[ 'Exchange.Server' ].'/EWS/Services.wsdl' );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM );
curl_setopt( $curl, CURLOPT_USERPWD, $cfg[ 'Exchange.User' ].':'.$cfg[ 'Exchange.Password' ] );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
echo '<pre>';
$response = curl_exec( $curl );
$info = curl_getinfo( $curl );
var_dump( $info );
var_dump( $response );
curl_close( $curl );
The result I receive here is the mentioned 401 status code and an empty response
When I call the same url in my browser or with the same code on my windows machine, I get the WSDL file I want
Actually I can't even tell if this is a linux-based problem or if I do something wrong at some point, I'm struggling with this for 2 days now.
Is there someone that may be able to find my mistake or tell me the reason why it doesn't work?
I may provide any further needed information on demand
If you initialize your soap client properly, you should be able to preform any requests requests this way:
$curl = curl_init($location); //'https://'.$server_address.'/EWS/Exchange.asmx'
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //valid soap headers with keep-alive
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
$response = curl_exec($curl);
As to your code, try commenting out following line:
curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM );
Also take a look wherever this wrapper works on your setup:
http://ewswrapper.lafiel.net/
If it does, take a look at SOAP classes used there - it uses php built-in as base.
Why can't you store wsdl file locally anyways?
UPDATE:
Ok, I played around with this in my Debian box and this works for me flawlessly:
$domain = 'xxxxxx';
$user = 'xxxxxx';
$password = 'xxxxxx';
$ch = curl_init('https://'.$domain.'/EWS/Services.wsdl');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);
$response = curl_exec($ch);
$info = curl_getinfo( $ch );
$error = curl_error ($ch);
print_r(array($response,$info,$error));
returns
Array
(
[0] => <?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions
(...)
</wsdl:definitions>
[1] => Array
(
[url] => xxxxx/EWS/Services.wsdl
[content_type] => text/xml
[http_code] => 200
[header_size] => 250
[request_size] => 147
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.60574
[namelookup_time] => 0.165249
[connect_time] => 0.268173
[pretransfer_time] => 0.474009
[size_upload] => 0
[size_download] => 55607
[speed_download] => 91800
[speed_upload] => 0
[download_content_length] => 55607
[upload_content_length] => 0
[starttransfer_time] => 0.580931
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] =>
)
[2] =>
)
Your first check should not use any complex scripting.
Instead try opening the WDSL from a simple browser window on the Debian machine like so:
https://your.exchange-server.com/EWS/Services.wsdl
If that does not work there are probably access restrictions in place that depend on the client IP or client network (e.g. your development machine being in a trusted network, your Debian not). The fact that you get a 401 ("Unauthorized" - request requires user authentication) suggests that there is no problem with contacting the server but with authentication.
Another check I suggest is that you have a look into your phpinfo() to make sure your PHP installation on Debian is capable of handling HTTP*S* requests. Make sure, OpenSSL is installed!
This article helped point me in the right direction. One thing to keep in mind is that
your PHP installation may not be sharing your system's cURL / libcurl setup.
http://blog.ianty.com/ubuntu/exchange-web-services-ews-ntlmv2-and-linux/
Related
I am hoping to build a video upload using the Streamable API and PHP with cURL. https://streamable.com/documentation#upload-video-file
What I'm trying to accomplish is:
User fills out a form of info and selects a video file from their computer/device to upload
Submits the form, PHP handles it from there to talk to the Streamable API to upload the video via the form to my Streamable account, then return the shortcode from Streamable for me to store in a MySQL database with the rest of their info
I've tried, with success, using the curl command via terminal. But, I'm having issues with pulling it off via php form submission.
This is an example of the command I used in terminal to upload, which worked:
curl https://api.streamable.com/upload -u my_email:my_pass -F file=#path/to/file.mp4
With PHP, I have a pretty simple cURL script thanks to the tons of help online. I guess you could say I'm pretty new to using cURL.
$url = 'https://api.streamable.com/upload -u my_email.com:my_pass -F file=#path/to/file.mp4';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
With that, I'm getting a HTTP 400 error.
https://streamable.com/documentation#errors
..codes in the 400 range indicate client errors...
I guess that's what's messing me up here?
I tried it this way, but I get the same error.
$pass = 'my_email:my_pass';
$postFields = array(
'file' => '/path/to/file.mp4',
'title' => 'Example Title'
);
$url = 'https://api.streamable.com/upload';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
I used print_r(curl_getinfo($ch)); to see what's happening, and this is what it's spitting out - maybe this can be useful for some help:
Array ( [url] => https://api.streamable.com/upload
-u my_email:my_pass -F file=#/Path/to/file.mp4 [content_type] => [http_code] => 400 [header_size] => 66 [request_size] => 277 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.36033 [namelookup_time] => 0.00138 [connect_time] => 0.082871 [pretransfer_time] => 0.283219 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [upload_content_length] => 275 [starttransfer_time] => 0.36031 [redirect_time] => 0 [redirect_url] => [primary_ip] => xx.xx.xxx.xxx [certinfo] => Array ( ) [primary_port] => 443 [local_ip] => 192.168.0.1 [local_port] => 57288 )
There are couple of things you need to change here:
Set CURLOPT_SSL_VERIFYPEER to false. It can be used to verify peer's certificate. If we specify it as false, it will accept any server(peer) certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Here's a good read on the implication of turning CURLOPT_SSL_VERIFYPEER on and off, If CURLOPT_SSL_VERIFYPEER is false, is the data transfer no longer secure?
CURLOPT_POSTFIELDS is used to specify full data we want to submit with the POST request. The $postFields array should be converted to URL-encoded query string using http_build_query() function, so that it could be sent as application/x-www-form-urlencoded.
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
Try this:
$fields = array("file"=>curl_file_create("video.mp4"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.streamable.com/upload");
curl_setopt($ch, CURLOPT_USERPWD, "email:pass");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec($ch);
I think curl_file_create makes the trick, it seems using #fileName no more work for PHP 5.5+
I have a php page that does an order lookup. In the order summary page, I do a while loop to bring up all of the orders. Normally doing this is just fine and isn't an issue. Recently I decided to add the ability to see an order "status" based on its JIRA issue/ticket status. So for instance, if the JIRA ticket state is open, then the order status is "open" as well. If it the JIRA ticket is closed, the order status is "closed" as well.
The way I am doing JIRA ticket lookups is via a CURL GET request. Here is an example of the code:
// CHECK JIRA TICKET
$username = 'xxxx';
$password = 'xxxx';
$proxy = 'http://xxxx.xxxx.xxxx:8080/';
$url = "http://xxxx.xxxx.xxxx/rest/api/2/issue/".$row['jira_ticket']."?fields=status";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$ticket_result = curl_exec($ch);
curl_close($ch);
In order for me to get a JIRA ticket status for each order, I am running this CURL GET request within a while loop. Example of code:
$results = mysqli query...;
while ($row = mysqli_fetch_array($results))
{
....define some variables
....the CURL code
....etc
}
The issue I am having is that the more orders I have, the longer it takes to load the page. I know for certain this is due to the CURL GET request within the while loop, because if I remove it, then the page loads quickly. When I debug the CURL request, the longest time I am seeing is starttransfer_time. Example:
[http_code] => 405
[header_size] => 639
[request_size] => 239
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.294348
[namelookup_time] => 0.004216
[connect_time] => 0.004885
[pretransfer_time] => 0.004928
[size_upload] => 0
[size_download] => 1013
[speed_download] => 3441
[speed_upload] => 0
[download_content_length] => 1013
[upload_content_length] => -1
[starttransfer_time] => 0.294315
[redirect_time] => 0
Sometimes it can go up to .5 seconds. So my question...is there any way I can speed up the CURL request? If not, is there a better way to approach what I'm attempting to do?
EDIT:
I have a workaround, though not really an answer. For the time being I will just store the "order status" as a column in MySQL, and have the actual JIRA CURL request in a different file run as a background cron job that runs every so often. It's not perfect, but at least this allows for the pages to come up much quicker.
I am working on Canvas LMS and have access token. I need to create an user account using web service in PHP. I have tried to do it using CURL (post method) but getting an error in response. However GET is working fine.
Like if I need to get information about course etc, it's working fine but account creation not working using CURL (post). Below is my code.
$url = "https://xxxxx.com/api/v1/accounts/2/users";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' .$token ) );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'name' => 'vaue',
'short_name' => 'value',
'unique_id' => '1121',
));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
Error:
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => An error occurred.
[error_code] => internal_server_error
)
)
[error_report_id] => 1124
)
I resolved my issue. The reason of "internal server error" was not sending required fields. Here are the required fields if someone need to know.
'user[name]' => '',
'user[terms_of_use]' => 'true',
'pseudonym[unique_id]' => '',//i.e valid email
'pseudonym[send_confirmation]'=>'true'
Now my CURL request is working fine and I am able to create an account successfully.
It looks like the keys for your arguments are incorrect. They should be:
'user[name]' => 'vaue',
'user[short_name]' => 'value',
'pseudonym[unique_id]' => '1121',
You can find the docs for your canvas install at: "https://{your canvas domain}/doc/api/index.html" or if you are using cloud hosted canvas at "api.instructure.com"
I'm working on a script to upload files to http://imagerelay.com. This is my first time using a REST API and cURL with PHP. There are two phases to the upload. First you submit metadata to the ImageRelay which includes the filesize for your local file. This returns an ID number which you use to build the URL for uploading the chunks (using CLI 'split' to generate). However, when I upload a file using cURL there are an extra 219 bytes added to each file. I suspect this is related to how cURL works, but I'm not sure how to correct the overage without manually increasing the filesize in phase 1 by 219bytes * x number of chunks.
Here is the response array received from the cURL request. The actual file chunk size is 1M (1,048,576 bytes), whereas the response shows 219 bytes more at 1048795
[http_code] => 201
[header_size] => 583
[request_size] => 346
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 2.039208
[namelookup_time] => 1.2E-5
[connect_time] => 0.040014
[pretransfer_time] => 0.13825
[size_upload] => 1048795
[size_download] => 1
[speed_download] => 0
[speed_upload] => 514314
[download_content_length] => 1
[upload_content_length] => 1048795
[starttransfer_time] => 0.182222
Here's the relevant section of the code. $file represents the chunk, and $resource is pre-determined as it increments each chunk.
$ch = curl_init( $resource );
curl_setopt($ch, CURLOPT_POST, true );
$pdfData = array( 'file' => new CurlFile( "file-chunks/{$file}", 'application/octet-stream') );
curl_setopt($ch, CURLOPT_POSTFIELDS, $params );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
I appreciate any help that can be provided. I've been working on this for a week and scouring the internet but I can't figure out why the filesize of the chunk doesn't match the upload_content_length or size_upload. I do not have the same issue when doing this over the command line with curl (and not PHP over CLI). Thank you!
[edit]Removed typo in first sentence (accidental paste!)[/edit]
[edit2]Added bounty[/edit]
Try to send file like this
$headers = array(
'Content-Type: multipart/form-data'
);
$file = "/full/path/to/file.pdf"; // Full path to file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
Try to use the option CURLOPT_VERBOSE and check STDERR to see the complete payload.
You can also try to use WireShark to check what goes on the wire.
(It could be scary at first but fairly easy to use).
Hi I am trying to build php youtube api without a Zend function
this is what I have till now:
function upload() {
$files = $_FILES;
$name = $files['file']['name'];
$type = $files['file']['type'];
$size = $files['file']['size'];
$tmp_nm = $files['file']['tmp_name'];
$data = array('name' => 'Foo', 'file' => '#'.$tmp_nm);
print_r($_POST);
print_r($_FILES);
echo 'Size '.$size;
$headers = array(
"Authorization: AuthSub token=".$this->auth,
"GData-Version: 2",
"X-GData-Key: key=".$this->dev_key,
"Content-length: ".$size,
"API_XML_request"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/action/GetUploadToken');
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_REFERER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER,0);
if($this->get_info)
{
$this->curlget_info($ch);
}
$output = curl_exec($ch);
print_r($output);
return $output;
}
The errors I get:
Output 1
Array ( [token] => TOKEN ) Array ( [file] => Array ( [name] => 0016.png [type] => image/png [tmp_name] => D:\wamp\tmp\php178D.tmp [error] => 0 [size] => 4216 ) ) Size 4216
Google
Error
Length Required
POST requests require a Content-length header.
Output 2
Array ( [token] => TOKEN ) Array ( [file] => Array ( [name] => Film.wmv [type] => video/x-ms-wmv [tmp_name] => D:\wamp\tmp\php11D3.tmp [error] => 0 [size] => 96589 ) ) Size 96589
Google
Error
Length Required
POST requests require a Content-length header.
I am using this guide.
I am trying to solve this for 5 days and I asked couple irc channels and forums. A friend linked me here to ask, I hope someone will help me :))
I don't have a developer key so I can't help you out directly, but clearly Google has a problem with your http header so you have to find out what you're sending in the header, not the message body. The best way to do this is to inspect the packet on the wire as it leaves your machine.
So install Wireshark, start it up on your WAMP server, start capturing packets, do your test, and then look at the http connection in the packet. Make sure it's what you expect.
Or maybe there's a way for curl to write the packet to a file instead of the server for debugging purposes. I don't know.
Also it's a long shot (and would rely on them being out of spec), but I noticed that you and that other person you linked to have "Content-length". Try "Content-Length" to match the example.
Not sure if this is the answer, but in the example page, they put quotes around the authsub token:
Authorization: AuthSub token="DXAA...sdb8"
Maybe try that?