CURL get page emty?

Multi tool use
Multi tool use


CURL get page emty?



I have a very weird case as follows :



This my CURL function :


function get_data($url) {
$ch = curl_init();
$timeout = 500;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}



I have a txt file content link line by line. I get all that link to an array and tried to get content :


$itemLink = file($LinkFile);
if(empty($itemLink)){
echo "endFile";
exit();
}
echo $itemLink[0]; //https://stackoverflow.com
echo get_data($itemLink[0]);



The result return empty but when i tried put direct link to my function like this :


echo get_data('https://stackoverflow.com');



Of course i can get full page normally.



Anybody knows what going on ?





See if curl_getinfo() can help you debug your curl request. Does get_data(trim($itemLink[0])) help at all by any chance?
– Scuzzy
Jul 1 at 22:48



curl_getinfo()


get_data(trim($itemLink[0]))





trim($itemLink[0]) working perfect.Thank you very much !
– Dương Trần
Jul 2 at 2:17





I'll turn my comment into an answer, so you can mark it as an accepted answer to help others.
– Scuzzy
Jul 2 at 4:45




2 Answers
2



Because you're using file() to read your document, I have a feeling you're getting the line endings in your URL and cURL is failing to handle the request.


file()



http://php.net/manual/en/function.file.php



Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.



to remove any superfluous line ending characters from the URL.


$itemLink = file( $LinkFile, FILE_IGNORE_NEW_LINES );



If this is not sufficient use the trim() function


trim()


echo get_data( trim( $itemLink[0] ) );



or


curl_setopt( $ch, CURLOPT_URL, trim( $url ) );



maybe a http > https thing?



Previously answered



curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);



source: How to use PHP CURL to bypass cross domain





I don't think this is the problem as the question asker states that get_data('https://stackoverflow.com') can be called.
– Scuzzy
Jul 1 at 22:54


get_data('https://stackoverflow.com')





a not recommended approach
– Muhammad Omer Aslam
Jul 1 at 23:00






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

n Z81AmlKvlh1h1XoZ,Lsp UPjJTG1DfLL
y3X1DXsU7IW4GJ q5Vn s,T hWI,oS6,u0

Popular posts from this blog

Boo (programming language)

Rothschild family