Posts Tagged curl

Verify the iTunes Account access information with the help of cURL

<?php
// URLs of iTunesconnect.
$urlBase = 'https://itts.apple.com';
$urlWebsite = $urlBase . '/cgi-bin/WebObjects/Piano.woa';
 
// function to check finance role acounts validity. This function return true is the detail is still valid.
function isAccountValid($username, $password) {
	global $urlBase, $urlWebsite;
 
	$html = curlRequest($urlWebsite, null); // First cURL request to get the login form
 
	preg_match('/" action=".*"/', $html, $matches); // Search the form action URL because all URLs are dynamic
 
	$urlActionLogin = $urlBase . substr($matches[0], 10, -1);
 
	$postData = array('theAccountName' => $username, 'theAccountPW' => $password, '1.Continue.x' => '1', '1.Continue.y' => '1'); // Creating the form POST data
 
	$html = curlRequest($urlActionLogin, $postData); // Second cURL request with form data for login
 
	if (strpos($html, 'Report Options') === false) { // If the return HTML contain 'Report Options' text then its successful login
		return false;
	}
 
	return true;
}
 
// function send the curl request on given URL with post variables and return the html output
function curlRequest($url, $postVars) {
	$ch = curl_init();
 
	curl_setopt($ch, CURLOPT_URL, $url);
 
	if (!is_null($postVars)){ 
		curl_setopt($ch, CURLOPT_POST, count($postVars));
 		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postVars));
	}
 
 	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
 	curl_setopt($ch, CURLOPT_HEADER, FALSE);  
 	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 
 	$html = curl_exec($ch);
 
	if (curl_errno($ch)) {
		die(curl_error($ch));
	}
 
	curl_close($ch);
 
	return $html;
}
 
// Examples 01
if (isAccountValid('anil@test.com', 'test@anil')) {
	echo "anil@test.com account is valid.<br/>";
}
else {
	echo "anil@test.com account is not valid.<br/>";
}
 
// Examples 02
if (isAccountValid('invalid_address@test.com', 'wrong_password')) {
	echo "invalid_address@test.com account is valid.<br/>";
}
else {
	echo "invalid_address@test.com account is not valid.<br/>";
}
 
?>

, ,

No Comments


How use cURL library when running PHP through Command Line

If you run any PHP code which using cURL extension through command line like this:

c:\>php somefile.php

it might give error that “curl_init()” function is not defined. The reason is the cURL extension “php_curl.dll” is not loaded. So, to resolve this issue you can load cURL (or any extension) dynamically in your PHP code. The “dl()” basically load PHP extension at runtime.

<?php
dl("php_curl.dll");
...
?>

,

No Comments


How upload file using cURL?

Here I am giving an example of how you can upload file using cURL in PHP.

Code:

<?php
	$request_url = ‘http://www.akchauhan.com/test.php’;
        $post_params['name'] = urlencode(’Test User’);
        $post_params['file'] =@.'demo/testfile.txt’;
        $post_params['submit'] = urlencode(’submit’);
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $request_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
        $result = curl_exec($ch);
        curl_close($ch);
?>

Here we have first initialized the new CURL session using “curl_init” function. Then we have set few curl options for CURL session.

CURLOPT_URL: URL to fetch.

CURLOPT_POST: TRUE to regular HTML POST.

CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of “curl_exec” instead of outputting it out directly.

CURLOPT_POSTFIELDS: The full data to post in a HTTP “POST” operation.

You can also set more options as per your requirement but these are the minimum require options for file upload. You can find more information about these options in PHP manual.

Then execute the CURL session through “curl_exec” function. This function will return the output of request.

Here the tricky part is ‘@’ symbol before file name. CURL automatically upload the give file to server or requested URL when it find the ‘@’ symbol in starting of file name.

Note: If you try this code on localhost under windows operating system then you might get the error message “CURL is failed to created the post data”. But this code is work fine under Linux environment. So, try this on your server.

The requested file “test.php” consider this request as simple POST FROM request. Like if you use this form for request.

<form method=”post” action=”test.php” enctype=”multipart/form-data”>
	<input type=textname=namevalue=”Test User” />
	<input type=”file” name=”file” />
	<input type=”submit” name=”submit” value=”submit” />
</form>

, ,

4 Comments