将数据从iPhone应用程序发布到php网页

时间:2013-11-28 11:22:51

标签: php ios iphone

我从iphone应用程序发布数据的问题是,当我从浏览器使用帖子访问url并通过www.apple.com然后它显示结果但是当我从iphone代码传递时它不会返回数据中的任何内容在下面的代码。

NSString * URL=@"www.apple.com";


NSLog(@"User Name entered is %@",URL);


NSString *post =[[NSString alloc] initWithFormat:@"URL=%@",URL];


NSLog(@"post is %@",post);

NSURL *url=[NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/urlTest.php?"];


NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];


NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];


NSLog(@"%@",data);

data = [data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",data);

这是PHP文件代码

<?php
function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}






$_URL=$_GET['URL'];




$html = file_get_contents_curl($_URL);


$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:

$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description' || $meta->getAttribute('name') == 'Description' )
        $description = $meta->getAttribute('content');
}

echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';



?>

这只是在iphone中返回

Title: <br/><br/>Description: <br/><br/>
2013-11-28 06:18:05.614 Hivelet[3861:60b] Title: <br/><br/>Description: <br/><br/>

0 个答案:

没有答案