Timeout when passing variables with the url and fopen() in php - php

I have done quite a bit of searching and cannot quite find my answer. My problem is that I am trying to call a link with GET variables attached to it and it just hangs and hangs until connection times out. When I just literally call the link in a web browser it works fine no problem.
Here is the fopen() php code example:
<?php
$url = "https://www.mysite.com/folder/second_folder/file.php?varA=val1&varB=val2&varC=val3&varD=val4&varE=val5";
$ch = fopen($url, 'r');
if(!$ch){
echo "could not open!!! $url";
} else {
echo "Success! ($url)";
}
?>
I can call file.php without the GET variables just fine. Returns with no error.
NOTE: I will say that file.php with one of the var's that get passed, does some functions and then does a header Location rewrite. I do not think it is even getting to this point when it does a connect timeout though because when I had problems, I put in a "check point" prior to the header Location point which should email me, and it does not email me.
Again, if I run the URL in a web browser it works just fine.
So what is going on if anyone can help me? I just need to run the URL as if PHP is clicking on the links. I have used fopen before but for some reason it does not work now. Also cURL did not work on this.

Try changing '' to " " in this case.
My working code is
<?php $handle = fopen("c:\\folder\\resource.txt", "r"); ?>

I think you want to be using
$ch = file_get_contents($url);
Edit: cURL option
// open
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page_data = curl_exec($ch);
$page_info = curl_getinfo($ch);
// close
curl_close ($ch);

Related

CURL in php returns a display text in my page

Hi i have this curl to download link files from the sendthisfile.com. Now my problem is when I try to test it last week my code runs well. But now when I test it this is what I got to my web app. I suspect this is from the curl when processing the files. Can someone help me figured this thing out? this is my code below in curl
$zipUrl = "https://download.com/sample.zip";
$fileName = date().".zip"; //create a random name or certain kind of name here
$fh = fopen($filename, 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $zipUrl);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
curl_close($ch);
can someone help me why this throws a texts in a webpage?
You're missing this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
This tells curl to return the output instead of directly displaying it. It's also noteworthy that you should set CURLOPT_RETURNTRANSFER before CURLOPT_FILE as I've read here.
See the official documentation for more information.

curl put file from server to bullhorn using php

I am trying to use curl and php to send a file from my liquid web server to the bullhorn vms system. I have tried multiple ways to get this to work but the current code i am using is:
$url_path_str = 'https://rest91.bullhornstaffing.com/rest-services/(corp token)/file/Candidate/'.$entityid.'/raw?filetype=SAMPLE&externalID=portfolio?BhRestToken='.$bhresttoken;
$file_path_str = '/home/path/'.$row["first_name"].$row["last_name"].'.pdf';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
echo $url_path_str;
echo $curl_response_res;
curl_close($ch);
fclose($fh_res);
The $entityid, $bhresttoken, $row["first_name"], and $row["last_name"] are variables I have created and using echo shows that they are working. Two errors show up separately when I try to execute this code.
The first error is
"{"errorMessage":"Missing 'BhRestToken' in request header or parameter.","errorMessageKey":"errors.authentication.missingRestToken","errorCode":412}"
which is weird because the bhresttoken allegedly isn't required but also it is definitely showing correctly at the end of the "url_path_str", so is it in the wrong spot?
The other issue that I get is
"An error occurred while processing your request. Reference #179.24343217.1582146766.1f0b67".
From what I've been told the reference number is a generic issue so I'm guessing that my code is flawed somewhere however I cannot figure out what is wrong.
Any advice or input would be much appreciated. Thank you in advance.

How to run php script

I have 7-8 php scripts written which pulls data from remote server and store it into our server. Each script insert/update around 3000-4000 records at a time. When I hit any script from browser it works fine(individual script) but if I try to call all files together by writing header('Location: http://www.example.com/') it gets break. Can anyone suggest me a better way to work with this. Someone suggested me use multi-threading I have not used threading yet so can anyone help me with the better approach/solution. TIA.
Note: your current code doesn't work because header('Location: example.com') redirects the browser to example.com which means your php script finished running and the browser is now on example.com
Solution 1:
if allow_url_fopen is "On" in php.ini you can execute them as using:
<?php
$url1 = file_get_contents('http://www.example.com/1.php');
$url2 = file_get_contents('http://www.example.com/2.php');
?>
and so on...
Solution 2:
function initCURL($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
use it as follows:
<?php
$url1 = initCURL('http://www.example.com/1.php');
$url2 = initCURL('http://www.example.com/2.php');
?>
in these examples $url1 and $url2 will carry whatever data is returned by the scripts.

How to get GitHub raw file contents with PHP?

I am browsing Github info/docs but cant find any simple example on how to
get contents of a raw Github file.
For example if I try to use
$url ="https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html";
echo file_get_contents($url);
I get failed to open stream: HTTP...
If I use curl same thing 404 page. So obviously I have to use API but there is just no simple example on how to do it.
Can someone please post plain example.
Thank you!
You can use a different method to escape SSL validation and add more options if you want, with CURL function.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
Works fine for me here, you just need to enable, if was not, the curl extension.

Simple_HTML_dom and Curl stopped working on a server

I've had several scripts pull information from a different server work perfectly for about a year.
Recently, it looks like they've changed something and both simple_html_dom and curl fail to work, either resulting in a failed to open stream: HTTP request failed! error or simply freezing.
Sometimes, I can successfully get ONE request through, but only one. The problem is the nature of the script requires more than one request from that server.
The page I am trying to pull is http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/cntnBscSrch.do?textField1=otimo&selectField1=tmlookup_ext&useblg=bscSrch.do%3Flang%3Deng&languageDirection=f&lang=eng&submitButton=Search&selectMaxDoc=1000000&selectDocsPerPage=1000000
Would really really appreciate any help
This is the simplified version of the code which also results in the same problem:
<?php
require("simple_html_dom.php");
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$link="http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/cntnBscSrch.do?textField1=otamo&selectField1=tmlookup_ext&useblg=bscSrch.do%3Flang%3Deng&languageDirection=f&lang=eng&submitButton=Search&selectMaxDoc=1000000&selectDocsPerPage=1000000";
$html = file_get_html($link);
echo "SIMPLE HTML DOM:<br>".$html;
$html = file_get_contents_curl($link);
echo "CURL:<br>".$html
?>

Categories