Extracting from array using PHP - php

I am learning PHP and I'm trying to understand the array below,
I need to grab content-type, it should give me
text/xml;charset=UTF-8 and then
grab the encoding which is UTF-8
Anyone good with arrays could you please help?
array(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 30 Aug 2014 17:10:12 GMT
[2] => Server: Apache
[3] => X-Gas_TTL: 900
[4] => Cache-Control: max-age=900
[5] => X-GasHost: gas1.usw
[6] => X-Cooking-With: Gasoline-Local
[7] => X-Gasoline-Age: 844
[8] => Last-Modified: Sat, 30 Aug 2014 16:56:08 GMT
[9] => Content-Type: text/xml;charset=UTF-8
[10] => Vary: Accept-Encoding
)
Thank you in advance
Regards,
Mona

.*?Content-Type:\s*(.*?=(.*?))\n.*
With s flag on this will give what's required.
See demo.
http://regex101.com/r/bJ6rZ5/1

The array must be in a variable ( I assume, since you seem to have printed it ), so you simply print/echo the value you're after.
echo $someArrayVariable[9];
Where someArrayVariable is a variable that contains the array.
That is assuming it is in a variable in the first place.

Related

I'm writing a links-checker in php and I can't get the HTTP error (e.g. 404 or 400) when the site displays a screen like 'this page can't be found'

I am making a links-checker tool to avoid broken links in our site content and it works when the page doesn't exist or can't be loaded - except when the external site replaces it with a screen saying something like 'This page doesn't seem to exist. Search for the content you are looking for from our menu...'.
Apart from the html/css/js code for this tool, here is the main PHP code that checks the links
$headers = get_headers($url);
$headers = (is_array($headers)) ? implode( "\n ", $headers) : $headers;
$exists = (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
$status = (is_array($headers)) ? $headers[0] : $headers;
Then js use this information including $status but it's not returning error code when the external site shows a 'not found' screen (e.g. http://www.drdansiegel.com/resources/healthy_mind_platter).
You get back redirects before resolving to the 404. I would invert your logic, check to see if you have a 404 or 400 ever present.
$notexists = (bool)preg_match('#^HTTP/.*\s40[04]\s#mi', $headers);
Also, you should use the m modifier so the leading anchor matches each line, not the whole string.
Additionally, note a character class is a list of characters, you can't do groupings in it as you have. [(200|301|302)] says a (, 2, 0, 0 (again), |, 3, 0 (again), etc. are all allowed. You would write that as (200|301|302) if you wanted 200, 301, or 302 to be allowed characters. You could use a character class for the last integer on the redirect status code (and should add 7 and 8 to that as though are valid redirects as well). So it could be (200|30[1278]).
Here's what your $headers contained from example link:
Array
(
[0] => HTTP/1.1 301 Moved Permanently
[1] => Server: nginx
[2] => Date: Sat, 15 May 2021 01:57:44 GMT
[3] => Content-Type: text/html
[4] => Content-Length: 162
[5] => Connection: close
[6] => Location: https://www.drdansiegel.com/resources/healthy_mind_platter
[7] => HTTP/1.1 301 Moved Permanently
[8] => Server: nginx
[9] => Date: Sat, 15 May 2021 01:57:45 GMT
[10] => Content-Type: text/html; charset=UTF-8
[11] => Content-Length: 0
[12] => Connection: close
[13] => Expires: Sat, 15 May 2021 02:57:45 GMT
[14] => Cache-Control: max-age=3600
[15] => X-Redirect-By: WordPress
[16] => Location: https://drdansiegel.com/resources/healthy_mind_platter
[17] => HTTP/1.1 404 Not Found
[18] => Server: nginx
[19] => Date: Sat, 15 May 2021 01:57:46 GMT
[20] => Content-Type: text/html; charset=UTF-8
[21] => Connection: close
[22] => Vary: Accept-Encoding
[23] => Expires: Wed, 11 Jan 1984 05:00:00 GMT
[24] => Cache-Control: no-cache, must-revalidate, max-age=0
[25] => Link: <https://drdansiegel.com/wp-json/>; rel="https://api.w.org/"
)

Return from cURL tested with preg_match fails

From cURL I´m getting back an response array where I´d like to test the body against a regex pattern. Here is a sample array:
Array ( [body] => 9068205463|admin [headers] => Array ( [0] => HTTP/1.1 200 OK [1] => Date: Mon, 04 May 2015 16:45:56 GMT [2] => Server: Apache [3] => Vary: Accept-Encoding [4] => Content-Encoding: gzip [5] => Content-Length: 38 [6] => Connection: close [7] => Content-Type: text/html ) [engine] => cURL [cached] => )
Here is what my php if statement looks like:
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", $result['body'])) {
die ("preg_match failed");
}
Question: Why is Die() fired?
Testing pattern here works like expected.
http://www.phpliveregex.com/p/b2W
Strange as this is working on my localhost but not on a production server.
Php Version is: PHP 5.3.10-1ubuntu3.18 with Suhosin-Patch
You probably have some spaces in your value and that's the reason why it doesn't matches the pattern.
To fix this simply use trim() in preg_match(), e.g.
if (!preg_match("/^[0-9]{10}\|[a-zA-Z]+$/", trim($result['body']))) {
//^^^^^
die ("preg_match failed");
}

Cannot find image name and extension from encode url

I have a question would like to ask you, it was about php. My problem when I tried to use php function to extend image extension from an url which has form below :
http://lh3.googleusercontent.com/i_qpu5lXHddZgNaEbzEEz1CaArLCHEmVNuhwVOuDUl0aIyZHuez3s4Uf878y1n9CqB5rld2a7GSAoWzoMgrC
so , for above url is made by Google which is not show use the file name and extesion name. of course I have try to use this below function but still not work :
$image_name = basename($url);
could anyone help me.
If you are downloading the image, you can get the extension using finfo_file().
Else you can look for the content type in the headers sent by the server using get_headers()
example code
<?php
$url = 'http://lh3.googleusercontent.com/i_qpu5lXHddZgNaEbzEEz1CaArLCHEmVNuhwVOuDUl0aIyZHuez3s4Uf878y1n9CqB5rld2a7GSAoWzoMgrC';
print_r(get_headers($url));
?>
sample output
Array
(
[0] => HTTP/1.1 200 OK
[1] => Access-Control-Allow-Origin: *
[2] => ETag: "v1"
[3] => Expires: Wed, 22 Apr 2015 09:10:30 GMT
[4] => Cache-Control: public, max-age=86400, no-transform
[5] => Content-Disposition: inline;filename="unnamed.png"
[6] => Content-Type: image/png
[7] => Date: Tue, 21 Apr 2015 09:10:30 GMT
[8] => Server: fife
[9] => Content-Length: 20365
[10] => X-XSS-Protection: 1; mode=block
[11] => Alternate-Protocol: 80:quic,p=1
)

Infusionsoft API not returning orders by date

I am trying to integrate the Infusionsoft API and I am having some issues in retrieving orders. I need to get all the orders from yesterday and so far this is what I have done.
require_once("src/isdk.php");
$app = new iSDK;
if ($app->cfgCon("connectionName")) {
echo "connected<br/>";
echo "app connected<br/>";
$qry = array('DateCreated' => $app->infuDate('10/30/2014'));
$rets = array('Id', 'JobTitle', 'ContactId', 'StartDate', 'DueDate', 'JobNotes', 'ProductId', 'JobRecurringId', 'JobStatus', 'DateCreated', 'OrderType', 'OrderStatus', 'ShipFirstName', 'ShipMiddleName', 'ShipLastName', 'ShipCompany', 'ShipPhone', 'ShipStreet1', 'ShipStreet2', 'ShipCity', 'ShipState', 'ShipZip', 'ShipCountry');
$cards = $app->dsQueryOrderBy("Job", 100, 0, $qry, $rets, 'DateCreated', false);
echo "<pre>";
print_r($cards);
echo "</pre>";
} else {
echo "Connection Failed";
}
The connection and everything works fine and I am able retrieve orders using other fields like Id. But for some reason searching through date doesn't work. I am not getting any errors below is the response which I get.
xmlrpcresp Object
(
[val] => yes
[valtyp] => phpvals
[errno] => 0
[errstr] =>
[payload] =>
[hdrs] => Array
(
[server] => Apache-Coyote/1.1
[pragma] => no-cache
[cache-control] => no-cache, no-store
[expires] => Sat, 01 Nov 2014 00:19:59 GMT
[content-type] => text/xml;charset=UTF-8
[content-length] => 121
[date] => Fri, 31 Oct 2014 12:19:59 GMT
[set-cookie] => app-lb=3238199306.20480.0000; path=/
)
[_cookies] => Array
(
[app-lb] => Array
(
[value] => 3238199306.20480.0000
[path] => /
)
)
[content_type] => text/xml
[raw_data] => HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store
Expires: Sat, 01 Nov 2014 00:19:59 GMT
Content-Type: text/xml;charset=UTF-8
Content-Length: 121
Date: Fri, 31 Oct 2014 12:19:59 GMT
Set-Cookie: app-lb=3238199306.20480.0000; path=/
<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value>yes</value></param></params></methodResponse>
)
And the format of the date is below.
20141030T00:00:00
Can anyone please help me with this?
I have also gone through similar questions and haven't found a solution.
Thanks in advance for help.
For those who run into the same problem below is the solution.
Querying for date using the code below solve the problem for me.
$qry = array('DateCreated' => '2014-10-30%');

Checking if a URL returns a PDF

I need to check if a URL returns a PDF document using PHP. Right now I'm using the file_get_mimetype function. A normal URL(https://www.google.com/) returns type as application/octet-stream while a normal pdf link (http://www.brainlens.org/content/newsletters/Spring%202013.pdf) returns application/pdf. But now I also encounter URL's like http://www.dadsgarage.com/~/media/Files/example.ashx or http://www.wpdn.org/webfm_send/241 which also is pdf but returns application/octet-stream. There are other URL's too which opens a Save as dialog box which also has to be detected.
Use get_headers()
Eg:
$url = "http://www.dadsgarage.com/~/media/Files/example.ashx";
if (in_array("Content-Type: application/pdf", get_headers($url))) {
echo "URL returns PDF";
}
print_r(get_headers($url));
returns
Array
(
[0] => HTTP/1.1 200 OK
[1] => Cache-Control: private, max-age=604800
[2] => Content-Length: 194007
[3] => Content-Type: application/pdf
[4] => Expires: Tue, 09 Dec 2014 09:40:20 GMT
[5] => Last-Modified: Wed, 07 Aug 2013 16:46:30 GMT
[6] => Accept-Ranges: bytes
[7] => Server: Microsoft-IIS/8.0
[8] => Content-Disposition: inline; filename="example.pdf"
[9] => X-AspNet-Version: 4.0.30319
[10] => X-Powered-By: ASP.NET
[11] => X-Provider: AWS
[12] => Date: Tue, 02 Dec 2014 09:40:20 GMT
[13] => Connection: close
)
mime types could include:
application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf

Categories